dousui6488 2017-11-16 23:00 采纳率: 100%
浏览 10

如何在go中表达C ++逻辑的“新”运算符?

I have a structure like this

type Node struct {
    data int
    next *Node
}

var root Node;

I would like to create a tmp Node and pass the address to root.next, how to write this kind of logic in go?

root.next = Node
  • 写回答

1条回答 默认 最新

  • doujiang3997 2017-11-16 23:03
    关注

    There are no constructors in Go. You just create an object by using the type name and you can set the fields at the same time.

    tmp := Node {
        data: 1
    }
    root.next = &tmp
    

    You can also take a pointer to a new object.

    tmp := &Node {
        data: 1
    }
    root.next = tmp
    

    And then put it all together.

    root.next = &Node {
        data: 1
    }
    

    There is also a new operator which is equivalent to &Node{} and therefore it is not very convienent as you need to assign field values later.

    评论

报告相同问题?

悬赏问题

  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂