go包裏面的變量和函數的首字母大寫表示爲public的方法或變量
/**
golang func
*/
package main
import (
"fmt"
)
var p = fmt.Println
var pf = fmt.Printf
//簡單示例
func add(x, y int) int {
return x + y
}
func sub(x, y int) int {
return x - y
}
//函數返回多個參數
func swap(x, y int) (int, int) {
return y, x
}
//注意下面這種寫法
func swap2(x, y int) (a, b int) {
a = y
b = x
return
}
//函數作爲參數 注意參數的寫法
func testFuncArguement(x, y int, f func(x, y int) int) int {
return f(x, y)
}
func main() {
p("5 + 3 =", add(5, 3))
a, b := 5, 3
a, b = swap(a, b)
pf("a=5 b=3 swap a=%d b=%d \n", a, b)
a, b = swap2(a, b)
pf("a=5 b=3 swap a=%d b=%d \n", a, b)
p("5 + 3 =", testFuncArguement(5, 3, add))
p("5 - 3 =", testFuncArguement(5, 3, sub))
}