/** * Created by Administrator on 13-12-18. */ package main import ( "fmt" "os" ) func f1(args ...interface {}) { f2(args...) f2(args[1:]...) } func f2(args ...interface {}) { for i, v := range args { fmt.Fprintf(os.Stdout, "i = %d %v\n", i, v) } fmt.Fprintf(os.Stdout, "--------------\n") } func main() { f1(1, "hello", 3.14, main) // 匿名函数 1 f := func(i, j int) (result int) { // f 为函数地址 result = i+j return } fmt.Fprintf(os.Stdout, "f = %v f(1,3) = %v\n", f, f(1, 3)) // 匿名函数 2 x, y := func(i, j int) (m, n int) { // x y 为函数返回值 return j, i }(1, 9) // 直接创建匿名函数并执行 fmt.Fprintf(os.Stdout, "x = %d y = %d\n", x, y) }
i = 0 1
i = 1 hello
i = 2 3.14
i = 3 0x4012c0
--------------
i = 0 hello
i = 1 3.14
i = 2 0x4012c0
--------------
f = 0x401690 f(1,3) = 4
x = 9 y = 1
有疑问加站长微信联系(非本文作者)