函数 1.声明语法:func 函数名 (参数列表) [(返回值列表)] {} 2.golang函数特点: a. 不支持重载,一个包不能有两个名字一样的函数 b. 函数是一等公民,函数也是一种类型,一个函数可以赋值给变量 c. 匿名函数 d. 多返回值 定义函数类型type: package main import "fmt" type add_func func(int, int) int func add(a, b int) int { return a + b } func sub…
创建函数 package main import "fmt" //有参数,有返回值 func demo(a int, s string) (int, string) { return a * a, s + s } //有参数,没有返回值 func test(a int) { fmt.Println(a) } //不需要参数,有返回值 func example() string { return "this is a str be returned" } func m…
不可或缺的函数,在Go中定义函数的方式如下: func (p myType ) funcName ( a, b int , c string ) ( r , s int ) { return } 通过函数定义,我们可以看到Go中函数和其他语言中的共性和特性 共性 关键字——func 方法名——funcName 入参——— a,b int,b string 返回值—— r,s int 函数体—— {} 特性 Go中函数的特性是非常酷的,给我们带来不一样的编程体验. 为特定类型定义函数,即为类型对象…