go 函数
发布时间:2020-12-16 09:33:14 所属栏目:大数据 来源:网络整理
导读:// func test(){// }// func test(a int,b int){// }// func test(a int,b int) int{// }func test(a int,b int ) (int,int){}func test(a,b int){} 花括号必须与函数声明在同一行,这种写法是错误的 func test(){} golang函数特点 1 不支持重载,一个包中不
// func test(){ // } // func test(a int,b int){ // } // func test(a int,b int) int{ // } func test(a int,b int ) (int,int){ } func test(a,b int){ } 花括号必须与函数声明在同一行,这种写法是错误的 func test() { } golang函数特点1 不支持重载,一个包中不能有两个名字一样的函数。 2. 函数也是一种类型,函数可以复制给一个变量。 3.支持匿名函数 4.多值返回 ? ? func test(a,b int) int { return a + b } func main() { c := test fmt.Println(c) sum := c(1,2) fmt.Println(sum) test(10,20) } ?自定义函数类型package main import "fmt" type ty_func func(int,int) int func add(a,b int) int { return a + b } func operator(op ty_func,a,b int) int { return op(a,b) } func main() { c := add sum := operator(c,100,200) fmt.Println(sum) } ? ? 函数参数传递方式1. 值传递 注意2:map、slice、chan、指针、interface默认以引用的方式传递 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |