Go by Example: Functions
来源:互联网 发布:淘宝直播电脑与手机 编辑:程序博客网 时间:2024/06/01 08:13
函数(Function)是Go的核心,我们将会通过多个不同的例子来学习函数。
package mainimport "fmt" // 这个函数输入两个int型数据,计算并返回int型的和func plus(a int, b int) int { // Go需要使用return语句显式地返回值 // 它不会自动返回函数中最后一个表达式的值。 return a + b}func main() { // 正如你所想的那样,通过“name(args)”来调用函数 res := plus(1, 2) fmt.Println("1+2 =", res)}
输出
$ go run functions.go 1+2 = 3
Go语言的函数还有其他一些功能。其中一个就是下一章节将会学习的多值返回。
下一个例子: Go by Example: Multiple Return Values。
英文原文
1 0
- Go by Example: Functions
- Go by Example: Variadic Functions
- Go by Example: Values
- Go by Example: Variables
- Go by Example: Constants
- Go by Example: For
- Go by Example: Switch
- Go by Example: Arrays
- Go by Example: Slices
- Go by Example: Maps
- Go by Example: Range
- Go by Example: Closures
- Go by Example: Recursion
- Go by Example: Pointers
- Go by Example: JSON
- 2.Go by Example: Values
- 3.Go by Example: Variables
- 4.Go by Example: Constants
- EditText讲解
- C++ STL--stack/queue 的使用方法
- 集群与分布式的区别
- [Java]读取文件方法大全
- Centos识别Windows NTFS格式分区
- Go by Example: Functions
- mahout入门指南
- You must supply a layout_width layout_width attribute 错误
- 单片机_C语言函数_中断函数(中断服务程序)
- 说明指针作为函数参数(一)
- Sonos完整体验:发烧音质,匠心之作
- APT-X 技术
- 【Qt编程】QWT在QtCreator中的安装与使用
- Java多态性理解