func 函数名(args…)返回值类型{

}

无参函数

  1. func foo(){
  2. fmt.Println("Hello,World!")
  3. }

有参函数

  1. func foo1(a string, b int) int {
  2. fmt.Println("a=", a)
  3. fmt.Println("b=", b)
  4. c := 100
  5. return c
  6. }

函数返回多个值

  1. // 匿名返回多个值
  2. func foo2(a int, b int) (int, int) {
  3. a = 100
  4. b = 200
  5. return a, b
  6. }
  1. // 返回多个值形参名称
  2. func foo3(a int, b int) (res1 int, res2 int) {
  3. // 给有返回值名称的变量赋值
  4. res1 = 1
  5. res2 = 2
  6. return
  7. }
  1. // 如果返回值类型相同 除了最后一个参数外其他的参数都可以省略类型
  2. func foo4(a int, b int) (res1, res2 int) {
  3. // 给有返回值名称的变量赋值
  4. res1 = 1
  5. res2 = 2
  6. return
  7. }