一、函数定义格式
func function_name ([parameter_List]) [return_types] {
}
二、函数定义及使用
package main
import (
"fmt"
"strconv"
)
func main() {
functionA()
str, number := functionB("这是个参数字符串", 999)
fmt.Println("main - functionB 返回的值:str:" + str + " number:" + strconv.Itoa(number))
strC, numberC := functionC()
fmt.Println("main - functionC 返回的值:strC:" + strC + " numberC:" + strconv.Itoa(numberC))
}
func functionA() {
fmt.Println("functionA: 无参数,无返回值的函数----")
}
func functionB(strA string, intA int) (string, int) {
fmt.Println("functionB: 有参数、有返回值的函数-----")
fmt.Println("functionB 接收到的参数 strA = " + strA + " intA = " + strconv.Itoa(intA))
return "functionB 返回的字符串", 10
}
func functionC() (resA string, resB int) {
fmt.Println("functionC: 返回值可以定义名称,可以在函数体内操作-----")
fmt.Println("functionC 返回值默认为默认初始化值 resA = " + resA + " resB = " + strconv.Itoa(resB))
resA = "函数体内给具名返回值赋值"
resB = 888
return resA, resB
}