go语言函数作为参数传递
2015-04-02 14:56
337 查看
go语言函数作为参数传递,目前给我的感觉几乎和C/C++一致。非常的灵活。
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
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
相关文章推荐
- go语言将函数作为参数传递
- go语言将函数作为参数传递
- Kotlin语言学习之将函数作为参数传递,使用双冒号::引用其他方法
- Go语言中函数的参数传递与调用的基本方法
- go数组作为函数参数传递
- C 语言的数组作为参数在函数之间的传递
- C语言技巧:怎样把数组作为参数传递给函数?
- go语言:函数参数传递详解
- go语言中将函数作为变量传递
- go语言:函数参数传递详解
- 如何将二维数组作为函数的参数传递
- 利用数组名作为函数参数传递排序和用指针进行排序
- 函数指针作为参数传递给函数
- C#中数组作为函数参数的传递方式
- 关于inline函数,可以作为函数参数传递的思考
- 多维动态数组作为函数传递参数
- 指针作为函数参数传递 (转载)
- 指针作为函数参数传递
- 指针作为函数参数传递
- 指针作为函数的参数传递的问题