Go by Example - Functions

翻译自 https://gobyexample.com/

Go by Example

Go is an open source programming language designed for building simple, fast, and reliable software.

Go by Example is a hands-on introduction to Go using annotated example programs. Check out the first example or browse the full list below.

Go by Example: Functions

函数是golang的核心。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package main

import "fmt"

// 接收两个int类型的参数,并返回两者之和。
// golang的函数需要显式的return。
func plus(a int, b int) int {
return a + b
}

// 多个同样类型的参数简便的定义。
func plusPlus(a, b, c int) int {
return a + b + c
}

func main() {
res := plus(1, 2)
fmt.Println("1+2=", res)

res = plusPlus(1, 2, 3)
fmt.Println("1+2+3=", res)
}
1
2
3
$ go run functions.go 
1+2 = 3
1+2+3 = 6

原文链接:Go by Example: Functions