function的两种
1 一种是普通的
函数是Go里面的核心设计,它通过关键字func
来声明,它的格式如下:
func funcName(input1 type1, input2 type2) (output1 type1, output2 type2) {
//这里是处理逻辑代码
//返回多个值
return value1, value2}
2 第二种,用来实现oo
method的语法如下:
func (r ReceiverType) funcName(parameters) (results)
我这里要补充一点
- r ReceiverType 如果是* r ReceiverType,即指针,那么原值修改。
package main import ( "fmt")
type object struct{ a int b int }
func (o *object) exchange()
{ o.a=o.a+o.b; o.b=o.a-o.b; o.a=o.a-o.b; }
func main()
{
var ac object
ac.b,ac.a=2,3
o:=object{1,2}
o.exchange()
fmt.Print(o,ac)
}