Go函式接收可變引數
簡單案例
package main
import(
"fmt"
"reflect"
)
func receiveSlice(manyints ...int) { //該函式可接收多個int引數, 打包為[]int型別slice
fmt.Println(reflect.TypeOf(manyints), len(manyints), cap(manyints))
fmt.Printf("%#v \n",manyints)
}
func receiveFourInts(var1, var2, var3, var4 int){
fmt.Printf("%#v%#v%#v%#v",var1, var2, var3, var4)
}
func main(){
receiveSlice(1,2,3,4)
receiveSlice(1,2,3,4,5,6,7,8)
slice := []int{1,2,3,4}
//receiveSlice(slice) //報錯
//receiveFourInts(slice...) //報錯, var...用法只能用於接收引數為 ...type的函式
receiveSlice(slice...) //正常
}
執行結果
[]int 4 4
[]int{1, 2, 3, 4}
[]int 8 8
[]int{1, 2, 3, 4, 5, 6, 7, 8}
[]int 4 4
[]int{1, 2, 3, 4}
使用場景
xorm 動態拼接資料的時候
//更新表2 eb_crm_good_check_log sql2, err2 := dbs.UpdateHelperObject.UpdateBatchByIds(updateGoodCheckData, mysql.EbCrmGoodCheckLogObject.TableName()) if nil != err2 { Logger.LoggersHelperObject.PanicAndLog("PushHDGoodControllerRun.148", err2) } _, err22 := session.Exec(sql2...)
本作品採用《CC 協議》,轉載必須註明作者和本文連結