go语言变参,匿名函数的多种用法

  • Post author:
  • Post category:其他


/**
 * 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



版权声明:本文为eclipser1987原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。