GoNote 发表于 2019-04-09 | 分类于 go | 字数统计 522 | 阅读时长 2 函数类型 匿名函数go 函数也是一种类型,用于声明变量,接收变量值 12345678910package mainimport "fmt"type myFunc func(a int, b int) intfunc main(){ var myfunc myFunc myfunc = func(a int, b int) int { return a+b } fmt.Println(myfunc(1,2))} 回调函数把函数当作一个参数 12345678910111213type myFunc func(a int, b int) intfunc calu(a int, b int, myfunc myFunc) int { return myfunc(a, b)}func main(){ var myfunc myFunc myfunc = func(a int, b int) int { return a+b } fmt.Println(calu(1,2, myfunc))} 闭包闭包中变量,一直存在 12345678910111213func test02() func() int{ vv := 1 return func() int{ vv++ return vv }}func main(){ f := test02() fmt.Println(f()) fmt.Println(f())} 匿名导入1import _ "fmt" 执行init函数 匿名字段继承 定义与初始化12345678910111213141516171819202122232425type people struct{ name string age int sex byte}type student struct{ people id int}func main() { s := student{people{"aa",16,'m'},150706} fmt.Println(s) s2 := student{people:people{name:"bb"} id:160706} fmt.Println(s2) // s3 := student{"aa",16,'m',150706} 错误用法 s4 := student{} s4.id = 170706 s4.name = "cc" s4.age = 17 s4.sex = 'm' fmt.Println(s4)} 非结构字段12345678910111213141516171819202122type people struct{ name string age int sex byte}type student struct{ people int}func main() { s := student{people{"aa",16,'m'},150706} fmt.Println(s) s4 := student{} s4.int = 170706 s4.name = "cc" s4.age = 17 s4.sex = 'm' fmt.Println(s4)} 同名字段就近原则 方法值与引用12345678910111213141516171819202122232425type people struct{ name string age int sex byte}func (this *people) setinfoPointer(){ this.name = "a" this.age = 12 this.sex = 'm'}func (this people) setinfo(){ this.name = "a" this.age = 12 this.sex = 'm'}func main() { p := people{} p.setinfo() // 无效 fmt.Println(p) p.setinfoPointer() fmt.Println(p)} 接口空接口 类型断言12345678910111213141516171819202122232425262728293031type people struct{ name string age int sex byte}func main() { ii := make([]interface{}, 3) ii[0] = 1 ii[1] = "2" ii[2] = people{name:"3"} for _,d := range ii { if val, ok:=d.(int); ok{ fmt.Println("int",val) }else if val, ok:=d.(string); ok{ fmt.Println("string",val) }else if val, ok:=d.(people); ok{ fmt.Println("people",val) } } for _,d := range ii { switch value := d.(type) { case int: fmt.Println("int",value) case string: fmt.Println("string",value) case people: fmt.Println("people",value) } }}