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
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
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.