func f1() {
fmt.Println("无参无返回值")
}
func f2(str string) {
fmt.Println("有参有返回值:" + str)
}
func f3(str1 string,str2 string) {
fmt.Println("有参有返回值:" + str1+ str2)
}
func f4(str string) string {
return "有参有返回值:" + str
}
func f5(str1,str2 string) (x,y string) {
fmt.Println("多参多返回值")
return
}
func f6(x int,y ...int) {
fmt.Println("不定参")
fmt.Println(y)
}
//将语句延迟 多个就逆序
func defer_bak() {
fmt.Println(1)
defer fmt.Println(2)
fmt.Println(3)
}
全局 函数作用于 局部(判断循环) 函数先内部后外部
func f1() {
}
func f2() int {
return 1
}
func f3(x int) int {
return 1
}
func f4(x func()int) {
}
func f5() func()int {
return f2
}
func main() {
fmt.Printf("%T \n", f1)
fmt.Printf("%T \n", f2)
fmt.Printf("%T \n", f3)
fmt.Printf("%T \n", f4)
fmt.Printf("%T \n", f5)
}
输出
func() func() int func(int) int func(func() int) func() func() int
func main() {
var a = func (x int) {
fmt.Println(x)
}
a(1)
func(){
fmt.Println("匿名")
}()
func(y string){
fmt.Println(y)
}("匿名")
}
func f1(x int) func(int) int {
return func(i int) int {
i += x
return i
}
}
func main() {
f11 := f1(100) //其实等于了 里面return的函数
fmt.Println(f11(10))
fmt.Println(f11(11))
}
func calc(index string,a,b int) int {
ret := a+b
fmt.Println(index,a,b,ret)
return ret
}
func main() {
a:=1
b:=2
defer calc("1",a,calc("10",a,b))
a = 0
defer calc("2",a,calc("20",a,b))
b=1
}
程序员,产品
8.7
PHP Go 前端
JwCode
渐悟分享