func (p mytype ) funcname ( a, b int , c string ) ( r , s int ) { return}
通过函数定义,我们可以看到go中函数和其他语言中的共性和特性
共性
关键字——func
方法名——funcname
入参——— a,b int,b string
返回值—— r,s int
函数体—— {}
特性
go中函数的特性是非常酷的,给我们带来不一样的编程体验。
为特定类型定义函数,即为类型对象定义方法
在go中通过给函数标明所属类型,来给该类型定义方法,上面的 p mytype 即表示给mytype声明了一个方法, p mytype 不是必须
的。如果没有,则纯粹是一个函数,通过包名称访问。packagename.funcationname
ype double float64//判断a是否等于bfunc (a double) isequal(b double) bool { var r = a - b if r == 0.0 { return true } else if r < 0.0 { return r > -0.0001 } return r < 0.0001}
推荐学习《golang教程》。
以上就是golang的函数怎么写的详细内容。