您的位置:首页 > 编程语言 > Go语言

go语言函数作为参数传递

2015-04-02 14:56 337 查看
go语言函数作为参数传递,目前给我的感觉几乎和C/C++一致。非常的灵活。

import "fmt"
import "time"

func goFunc1(f func()) {
go f()
}

func goFunc2(f func(interface{}), i interface{}) {
go f(i)
}

func goFunc(f interface{}, args... interface{}) {
if len(args) > 1 {
go f.(func(...interface{}))(args)
} else if len(args) == 1 {
go f.(func(interface{}))(args[0])
} else {
go f.(func())()
}
}

func f1() {
fmt.Println("f1 done")
}

func f2(i interface{}) {
fmt.Println("f2 done", i)
}

func f3(args... interface{}) {
fmt.Println("f3 done", args)
}

func main() {
goFunc1(f1)
goFunc2(f2, 100)

goFunc(f1)
goFunc(f2, "xxxx")
goFunc(f3, "hello", "world", 1, 3.14)
time.Sleep(5 * time.Second)
}


f1 done
f2 done 100
f1 done
f2 done xxxx
f3 done [[hello world 1 3.14]]

转自 http://blog.csdn.net/eclipser1987/article/details/11772539
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: