go语言基础之函数类型
1、函数类型
示例:
package main import "fmt" func Add(a, b int) int { return a + b } func main() { var result int result = Add(1, 1) fmt.Println("result = ", result) }
执行结果:
result = 2
2、自定义类型 (加法)
package main import "fmt" func Add(a, b int) int { return a + b } type FuncType func(int, int) int func main() { var result int var fTest FuncType fTest = Add result = fTest(10, 20) fmt.Println("result2 = ", result) }
执行结果:
result2 = 30
3、自定义类型 (减法)
package main import "fmt" func Minus(a, b int) int { return a - b } type FuncType func(int, int) int func main() { //减法 var result int var fTest FuncType fTest = Minus result = fTest(10, 5) fmt.Println("result3 = ", result) }
执行结果:
result3 = 5