小编vko*_*syj的帖子

在go中不能用作赋值类型

当我编译我的代码时,我收到以下错误消息,不知道为什么会发生.有人可以帮我指出原因吗?先感谢您.

不能在赋值时使用px.InitializePaxosInstance(val)(类型PaxosInstance)作为类型*PaxosInstance

type Paxos struct {
    instance   map[int]*PaxosInstance
}    

type PaxosInstance struct {
    value        interface{}
    decided      bool
}    

func (px *Paxos) InitializePaxosInstance(val interface{}) PaxosInstance {
    return PaxosInstance {decided:false, value: val}
}

func (px *Paxos) PartAProcess(seq int, val interface{}) error {  
    px.instance[seq] = px.InitializePaxosInstance(val)
    return nil 
Run Code Online (Sandbox Code Playgroud)

}

go

11
推荐指数
2
解决办法
2万
查看次数

使用javaScript动态创建表时无法分配id

[编辑] 感谢prasad和Vijayp的帮助,我解决了设置边框的问题.但是,另一个问题没有得到充分解决.我为没说清楚而道歉.我的问题是动态创建表后.你可以看到有5个horenzontal边界.(请参见下图)我想为每个honrizontal边界分配一个id.例如,我想在顶部边框上标识id为0; 第二个上边框的id为1等等但是,我不知道怎么做.希望有人可以帮助我.

在此输入图像描述

HTML:

/* js: */

$(document).ready(function() {
    var table = document.createElement('table');
    for (var i = 0; i < 4; i++){
	    var tr = document.createElement('tr');   
	    var td1 = document.createElement('td');
	    // assign the id
	    td1.id = i; 
	    tr.appendChild(td1);
	    table.appendChild(tr);
	    td1.className = "deco";
}
document.body.append(table);
	
});
Run Code Online (Sandbox Code Playgroud)
/* css: */

   
.deco {
    border: 1px solid black;
}

table {
  border-collapse:collapse;
  border: 1px solid black;
}

table, td, th {
  border: 1px solid black;
  padding: 10px 20px;
   }
Run Code Online (Sandbox Code Playgroud)
<!DOCTYPE html>
<html>
<head>
<link …
Run Code Online (Sandbox Code Playgroud)

html javascript css jquery

5
推荐指数
1
解决办法
106
查看次数

使用新的和频道去

我在下面的技术演讲中找到了代码片段,我对一件事情有点困惑.

应该table <- new(Ball)放在之前go player("ping", table)
为什么我们甚至需要table <- new(Ball)?我认为table := make(chan *Ball)已经创造了渠道.这与死锁有关吗?

type Ball struct { hits int }

fun main() {
    table := make(chan *Ball)
    go player("ping", table)
    go player("pong", table)
    table <- new(Ball) // game on; toss the ball
    time.Sleep(1*time.Second)
    <-table // game over; grab the ball
}

func player(name string, table chan *Ball)
    for {
        ball := <-table
        ball.hits++
        fmt.println(name, ball.hits)
        time.Sleep(100 * time.Millisecond)
        table <- ball
    }
}
Run Code Online (Sandbox Code Playgroud)

go

2
推荐指数
1
解决办法
60
查看次数

删除d3.js中的元素

NewBie for d3.js. 我尝试根据方法删除xAxis

.s.selectAll( "G")除去(x-轴);

但它不起作用.不确定是否是删除xAxis的正确方法?先感谢您.

1. var xAxis = d3.axisBottom()
    .scale(xScale);


2. s.append("g")
        .attr("class", "x axis")
        .attr("transform", "translate(0," + height + ")")
        .call(xAxis)

3. s.selectAll("g").remove(xAxis);
Run Code Online (Sandbox Code Playgroud)

javascript d3.js

2
推荐指数
1
解决办法
2171
查看次数

go语言的潜在竞争条件

我不确定为什么以下代码有竞争条件,有人可以给我一个提示吗?我认为没有潜在的竞争条件.先感谢您.

type PossiblySafeCounter struct {
    mu sync.Mutex
    sum int
}

func (c *PossiblySafeCounter) inc() {
   c.mu.Lock();
   defer c.mu.Unlock();
   go func() {
       c.sum++
   }() 
}
func (c *PossiblySafeCounter) read() int {
    c.mu.Lock();
    defer c.mu.Unlock();
    return c.sum
 }
Run Code Online (Sandbox Code Playgroud)

go

0
推荐指数
1
解决办法
78
查看次数

标签 统计

go ×3

javascript ×2

css ×1

d3.js ×1

html ×1

jquery ×1