聲明函數(shù)
func fn1(arg1 type,arg2 type) (output1 type,output2 type){
return output1 ,output2
}
func fn2(arg1 ,arg2 type) (output1 ,output2 type){
return output1 ,output2
}
- 可變參數(shù) ,arg2的類型為TS中的Array<string>
func fn3(arg1 int,arg2...string) (int,[]string){
return arg1 ,arg2
}
// 接收多個(gè)返回參數(shù)罗晕,_丟棄當(dāng)前數(shù)據(jù)
_,result :=fn3(1,"a","b")
- 值傳遞融痛,在調(diào)用函數(shù)時(shí)將實(shí)際參數(shù)復(fù)制一份傳遞到函數(shù)中忽你,這樣在函數(shù)中如果對(duì)參數(shù)進(jìn)行修改,將不會(huì)影響到實(shí)際參數(shù)。
type aType struct {
content string
}
a := aType{
content : "a"
}
func fn4 (a aType) aType{
a.content = "aa"
return a
}
result := fn4(a)
fmt.Println(a, result) // {a} {aa}
- 引用傳遞,在調(diào)用函數(shù)時(shí)將實(shí)際參數(shù)的地址傳遞到函數(shù)中栗菜,那么在函數(shù)中對(duì)參數(shù)所進(jìn)行的修改,將影響到實(shí)際參數(shù)蹄梢。
type aType struct {
content string
}
a := aType{
content : "a"
}
func fn4 (a *aType) aType{
a.content = "aa"
return *a
}
result := fn4(&a)
fmt.Println(a, result) // {aa} {aa}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者