新问题:Go语言怎么初始化结构体类型数组中的指针类型呢?(原问题:Go语言怎么在结构体中定义一个指向函数的指针呢?)

yelingyun · 2017-05-04 09:33:25 · 3529 次点击

后来我改数组用单链表了,数组加结构体比较麻烦,调试成功了。代码如下:

type DataNode struct{ cmd string desc string handler func()int next *DataNode }

var head = DataNode{} var head0 = DataNode{} var head1 = DataNode{}

func main(){

head.next = &head0

head0.cmd = "help"
head0.desc = "cmd tips"
head0.handler = help
head0.next = &head1

head1.cmd = "add"
head1.desc = "addition of two numbers"
head1.handler = add
head1.next = nil

...... }

#14
更多评论

func(*DataNode) next (){} 这样不好吗?

#1

你好,谢谢你的回答。是这样的,在结构体中,我定义handler是一个指向函数的指针变量,cmd是指向控制台输入的命令的指针变量。根据cmd值的不同,程序将不同的函数地址赋给handler。在c中,可以这么定义,int (*handler)(),但是我不知道怎么用go实现同样的功能,你知道吗?谢谢。

#2