函数 -- 就要学习 Go 语言

栏目: Go · 发布时间: 7年前

内容简介:函数是一段能够完成特定功能的代码段,可以接收输入参数或者能够返回想要的值。Go 语言采用如下语法声明一个函数:可以看到,

函数是一段能够完成特定功能的代码段,可以接收输入参数或者能够返回想要的值。

函数声明

Go 语言采用如下语法声明一个函数:

func funcName(parameterName type) returntype {
	//函数体 
}
复制代码

可以看到, func 是声明函数的关键字; funcName 是函数名; () 之间的是输入参数,根据需要,可传多个参数 (parName1 type, parName2 type) 或者不传; returntype 是返回值类型,如果没有返回值,可以省略。

func add(a int,b int) int {
	return a + b
}

func main() {
	fmt.Println("a + b =",add(1,2))
}
复制代码

输出:

a + b = 3
复制代码

上面的 add() 函数示例很简单:两数相加。 (){} 之间的 int 是返回值的类型。如果有不止一个相同类型的输入参数,则可以简写:

func add(a,b int,c,d string) int {
	fmt.Println(c+d)
	return a + b
}

func main() {
	fmt.Println("a + b =",add(1,2,"add"," + "))
}
复制代码

输出:

add + 
a + b = 3
复制代码

ab 都是 int 型,所有简写成 a,b intcd 都是 string 类型,同理。

多值返回

不像其他语言,Go 语言的函数是允许返回多个值的。

func calculate(a,b int) (int,int) {
	return a+b,a*b
}

func main() {
	sum,multi := calculate(1,2)
	fmt.Println("a + b =",sum)
	fmt.Println("a * b =",multi)
}
复制代码

上面的代码, calculate() 函数返回两个值,在调用函数中不需要用到其中某一个值的时候,可以使用空白标识符 _ 忽略。因为如果定义了变量而没有使用,会编译不通过。

func main() {
	sum,_ := calculate(1,2)
	fmt.Println("a + b =",sum)
	//fmt.Println("a * b =",multi)
}
复制代码

命名返回值

在函数定义的时候,可以给所有的返回值分别命名,Go 会自动创建这些变量,在函数体中可以给变量赋值等。在函数结束的时候,直接使用 return 就可以将返回值返回,而不用指定返回的变量。

func calculate(a,b int) (sum,multi int) {
	sum = a + b
	multi = a * b
	return      
}

func main() {
	sum,multi := calculate(1,2)
	fmt.Println("a + b =",sum)
	fmt.Println("a * b =",multi)
}
复制代码

注意:函数结束时的 return 是必须的,不能省略。

函数类型

Go 语言中,函数也是一种类型。如果两个函数的入参类型和返回值类型均相同,则它们是同种类型。如果将函数作为参数传递给另外一个函数时或将函数作为返回值时,函数类型就会显得很有用了。

func add(a,b int) (r int) {
	r = a + b
	return
}

func multi(c,d int) (res int) {
	res = c * d
	return
}

func main() {
	fmt.Printf("%T \n",add)
	fmt.Printf("%T",multi)
}
复制代码

输出:

func(int, int) int 
func(int, int) int
复制代码

定义了两个函数 add()multi() ,它们的入参和返回值都一样,所有它们的类型相同,都是 func(int, int) int

func add(a,b int) (r int) {
	r = a + b
	return
}

func multi(c,d int) (res int) {
	res = c * d
	return
}

func calculate(a,b int,f func(int,int) int) (r int) {
	r = f(a,b)
	return
}

func main() {
	sum   := calculate(1,2,add)
	multi := calculate(1,2,multi)
	fmt.Println(sum,multi)
}
复制代码

输出:

3 2
复制代码

上面的代码,定义 calculate() 函数,接收两个 int 类型的参数和一个 func(int,int) int 类型的参数 f ,在函数体里,调用了 f 函数。 可以给 func(int,int) int 创建类型别名,是程序更简洁:

func add(a,b int) (r int) {
	r = a + b
	return
}

func multi(c,d int) (res int) {
	res = c * d
	return
}
// 创建类型别名
type calculateFunc func(int,int) int
func calculate(a,b int,f calculateFunc) (r int) {
	r = f(a,b)
	return
}

func main() {
	sum   := calculate(1,2,add)
	multi := calculate(1,2,multi)
	fmt.Println(sum,multi)
}
复制代码

匿名函数

Go 语言中,函数可以作为一个值,这样便可以将函数赋给一个变量或者返回一个函数变量。

var add = func(a,b int) (r int) {
	r = a + b
	return
}

func main() {
	fmt.Println("a + b =",add(1,2))
}
复制代码

上面的代码,创建了一个匿名函数,赋给了一个全局变量 add 。Go 编译器会自动判别变量 add 的类型,这里是 func(int, int) int

匿名函数可以赋值,也可以立即执行:

func main() {
	sum := func(a,b int) int {
		return a + b
	}(1,2)
	fmt.Println(sum)
}
复制代码

输出:

3
复制代码

上面的代码,定义匿名函数时,直接传递了两个参数,返回结果。

关注公众号「Golang来了」,获取最新文章!

函数 -- 就要学习  <a href='https://www.codercto.com/topics/6127.html'>Go</a>  语言

以上所述就是小编给大家介绍的《函数 -- 就要学习 Go 语言》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Inside Larry's and Sergey's Brain

Inside Larry's and Sergey's Brain

Richard Brandt / Portfolio / 17 Sep 2009 / USD 24.95

You’ve used their products. You’ve heard about their skyrocketing wealth and “don’t be evil” business motto. But how much do you really know about Google’s founders, Larry Page and Sergey Brin? Inside......一起来看看 《Inside Larry's and Sergey's Brain》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

SHA 加密
SHA 加密

SHA 加密工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具