日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

Go語言中函數(shù)可變參數(shù)(Variadic?Parameter)詳解_Golang

作者:孫琦Ray ? 更新時(shí)間: 2022-09-09 編程語言

基本語法

在Python中,在函數(shù)參數(shù)不確定數(shù)量的情況下,可以使用如下方式動(dòng)態(tài)在函數(shù)內(nèi)獲取參數(shù),args實(shí)質(zhì)上是一個(gè)list,而kwargs是一個(gè)dict

def myFun(*args, **kwargs):

在Go語言中,也有類似的實(shí)現(xiàn)方式,只不過Go中只能實(shí)現(xiàn)類似*args的數(shù)組方式,而無法實(shí)現(xiàn)**kwargs的方式。實(shí)現(xiàn)這種方式,其實(shí)也是利用數(shù)組的三個(gè)點(diǎn)表達(dá)方式,我們這里來回憶一下。

關(guān)于三個(gè)點(diǎn)(…)Ellipsis的說明

我們經(jīng)常在Go中看到這種方式,首先三個(gè)點(diǎn)的英文是Ellipsis,翻譯成中文叫做“省略”,可能各位看到這個(gè)詞就比較好理解三個(gè)點(diǎn)的作用了。在不同的位置上有不同的作用,比如在上述數(shù)組的定義中,省略了數(shù)組長度的聲明,而是根據(jù)數(shù)組初始化值來決定。在函數(shù)定義中,我們還會(huì)看到類似的使用方法,我們?cè)龠M(jìn)行詳細(xì)的說明。

其實(shí)本質(zhì)上三個(gè)點(diǎn)的表達(dá)方式就是利用數(shù)組這一特性,實(shí)現(xiàn)可變參數(shù)。來看一下定義格式:

// arg will be [...]int
func myfunc(arg ...int) {}

// paras will be [...]string
func myfunc(arg, paras ... string) {}

示例一:函數(shù)中獲取可變參數(shù)

循環(huán)獲取可變參數(shù),并且將部分arguments傳入子函數(shù)

package main

import "fmt"

func myfunc(arg ... string) {
    fmt.Printf("arg type is %T\n", arg)
    for index, value := range arg {
        fmt.Printf("And the index is: %d\n", index)
        fmt.Printf("And the value is: %v\n", value)
    }
}

func main() {
    myfunc("1st", "2nd", "3rd")
}

對(duì)上面的例子進(jìn)行分析:

可變參數(shù)arg類型為[]string

通過for進(jìn)行循環(huán)并獲取值

arg type is []string
And the index is: 0
And the value is: 1st
And the index is: 1
And the value is: 2nd
And the index is: 2
And the value is: 3rd

示例二:將切片傳給可變參數(shù)

我們?cè)谏厦娉绦虻幕A(chǔ)上實(shí)現(xiàn)一個(gè)新的函數(shù)mySubFunc,嘗試將切片(Slice)傳遞給該函數(shù)

package main

import "fmt"

func myfunc(arg ... string) {
    fmt.Printf("arg type is %T\n", arg)
    for index, value := range arg {
        fmt.Printf("And the index is: %d\n", index)
        fmt.Printf("And the value is: %v\n", value)
    }

    // Call sub funcation with arguments
    fmt.Printf("Pass arguments: %v to mySubFunc\n", arg[1:])
    mySubFunc(arg[1:] ...)
}

func mySubFunc(arg ... string) {
    for index, value := range arg {
        fmt.Printf("SubFunc: And the index is: %d\n", index)
        fmt.Printf("SubFunc: And the value is: %v\n", value)
    }
}

func main() {
    myfunc("1st", "2nd", "3rd")
}

我們來分析一下這段代碼:

1.與上面的代碼大部分邏輯相同,這里利用切片arg[1:]獲取部分可變參數(shù)的值

2.在傳輸給子函數(shù)mySubFunc()時(shí),使用了這樣的表達(dá)方式mySubFunc(arg[1:] …),這里補(bǔ)充一下…對(duì)于切片用法的說明

… signifies both pack and unpack operator but if three dots are in the tail position, it will unpack a slice.

在末尾位置的三個(gè)點(diǎn)會(huì)unpack一個(gè)切片

示例三:多參數(shù)

我們?cè)賮砜匆粋€(gè)多參數(shù)的例子

package main

import "fmt"

func myfunc(num int, arg ... int) {
    fmt.Printf("num is %v\n", num)
    for _, value := range arg {
        fmt.Printf("arg value is: %d\n", value)
    }
}

func main() {
    myfunc(1, 2, 3)
}

來分析一下這個(gè)代碼:

函數(shù)參數(shù)一個(gè)為整型變量num,和可變變量arg

主函數(shù)中第一個(gè)參數(shù)為num,而后面的則存儲(chǔ)于arg中

所以輸出結(jié)果如下

num is 1
arg value is: 2
arg value is: 3

原文鏈接:https://blog.csdn.net/xiaoquqi/article/details/125816759

欄目分類
最近更新