普通函数
package main
import "fmt"
func main(){
r1,r2 :=a(0,"data2的数据")
fmt.Println(r1,r2)
}
func a(data1int, data2string)(ret1int,ret2string){
ret1 = data1
ret2 = data2
return
}
不定参数
package main
import "fmt"
func main(){
a(0,"1","2","三")
}
func a(data1int, data2 ...string){
fmt.Println(data1,data2)
for k,v :=range data2{
fmt.Println(k,v)
}
}
匿名函数
package main
import "fmt"
func main(){
a :=func(data1string) {
fmt.Println(data1)
}
a("我是匿名函数")
}
自执行函数
package main
import "fmt"
func main(){
(func() {
fmt.Println("我在这里执行,别人都别管我")
})()
}
闭包函数
package main
import "fmt"
func main(){
mo()(4)
}
func mo()func(int) {
return func(numint) {
fmt.Println(4)
}
}
延迟函数
package main
import "fmt"
func main(){
defer mo()
fmt.Println("1")
fmt.Println("2")
}
func mo() {
fmt.Println("我想最先执行")
}