golang 函数作为类型
2015-04-15 16:25
225 查看
golang 函数作为类型 package main import "fmt" type A func(int, int) func (f A)Serve() { fmt.Println("serve2") } func serve(int,int) { fmt.Println("serve1") } func main() { a := A(serve) a(1,2) a.Serve() }
type functinTyoe func(int) bool // 声明了一个函数类型 func isOdd(integer int) bool { if integer%2 == 0 { return false } return true } func isEven(integer int) bool { if integer%2 == 0 { return true } return false } // 声明的函数类型在这个地方当做了一个参数 func filter(slice []int, f functinTyoe) []int { var result []int for _, value := range slice { if f(value) { result = append(result, value) } } return result } func test(){ slice := []int {1, 2, 3, 4, 5, 7} fmt.Println("slice = ", slice) odd := filter(slice, isOdd) // 函数当做值来传递了 fmt.Println("Odd elements of slice are: ", odd) even := filter(slice, isEven) // 函数当做值来传递了 fmt.Println("Even elements of slice are: ", even) }
相关文章推荐
- 【GoLang】函数作为 类型 和 值
- 函数作为变量,类型---golang
- 指针类型作为成员函数的参数
- 将“引用”作为函数返回值类型的格式、好处和需要遵守的规则?
- string类型在参数传递中如何作为引用,如何在函数外修改函数内的string类型
- C# 引用类型作为函数参数时
- C++编程中将引用类型作为函数参数的方法指南
- const 作为函数返回类型
- 在COM/DCOM中如何将数组作为函数返回的类型传递
- Function类型-作为值的函数
- 类作为其自身成员函数参数时的类型
- 指针类型作为成员函数的参数
- 函数指针(函数指针作为函数形参/函数类型作为函数返回类型)
- OpenCV中将MAT类型的对象作为InputArray类型的对像传递给函数
- C++结构体类型作为函数参数
- go语言中函数作为值、类型
- C# 引用类型作为函数参数时
- C# 引用类型作为函数参数时
- QT 自定义信号与信号槽方法及解决自定义数据类型或数组作为函数参数的问题
- 函数作为返回值输出 判断数据类型