函数是语句序列的集合,能够将一个大的工作分解为小的任务,对外隐藏了实现细节
func name(parameter-list) (result-list){ body }
func plus(a, b int) (res int){ return a + b }
func multi()(string, int){ return "name", 18 }
// 被命名的返回参数的值为该类型的默认零值 // 该例子中 name 默认初始化为空字符串,height 默认初始化为 0 func namedReturnValue()(name string, height int){ name = "xiaoming" height = 180 return }
func sum(nums ...int)int{ fmt.Println("len of nums is : ", len(nums)) res := 0 for _, v := range nums{ res += v } return res } func main(){ fmt.Println(sum(1)) fmt.Println(sum(1,2)) fmt.Println(sum(1,2,3)) }
func main(){ func(name string){ fmt.Println(name) }("禾木课堂") }
func main() { addOne := addInt(1) fmt.Println(addOne()) fmt.Println(addOne()) fmt.Println(addOne()) addTwo := addInt(2) fmt.Println(addTwo()) fmt.Println(addTwo()) fmt.Println(addTwo()) } func addInt(n int) func() int { i := 0 return func() int { i += n return i } }
func sayHello(name string) { fmt.Println("Hello ", name) } func logger(f func(string), name string) { fmt.Println("start calling method sayHello") f(name) fmt.Println("end calling method sayHellog") } func main() { logger(sayHello, "禾木课堂") }
func sendValue(name string) { name = "hemuketang" } func sendAddress(name *string) { *name = "hemuketang" } func main() { // 传值和传引用 str := "禾木课堂" fmt.Println("before calling sendValue, str : ", str) sendValue(str) fmt.Println("after calling sendValue, str : ", str) fmt.Println("before calling sendAddress, str : ", str) sendAddress(&str) fmt.Println("after calling sendAddress, str: ", str) }
Copyright© 2013-2020
All Rights Reserved 京ICP备2023019179号-8