[Go] Golang用法 function input & output 參數可以同樣型態放一起

參數太長該怎麼辦,同樣類型的可以寫在一起。

這件事情我知道的時候還蠻訝異的,同時,又得知不只傳入的參數可以這樣,return的型態也可以這樣搞。

另外,若事先指定回傳的變數名稱,可以在function當中各處先assign你要的回傳值到這些變數。



Input的參數可以類型寫一起


    func GetMyData(name string, sex string, birthday string,id int, number int) {

    }

    // 可以改寫為

    func GetMyData(name, sex, birthday string, id, number int) {

    }



return的參數也可以類型寫一起,並且偷偷宣告變數名稱


    func GetMyData(name, sex, birthday string, id, number int) (bool,bool){

    }

    // 可以指定命名

    func GetMyData(name, sex, birthday string, id, number int) (isOK bool, isPassed bool){

    }

    // 當然也可以匯集

    func GetMyData(name, sex, birthday string, id, number int) (isOK, isPassed bool){

    }




function 中串插要回傳的值


    func GetMyData(name, sex, birthday string, id, number int) (bool, bool){
        isOK := true
        isPassed := false
        return isOK, isPassed
    }

    // 就可以改寫為

    func GetMyData(name, sex, birthday string, id, number int) (isOK, isPassed bool){
        isOK = true
    
        // 中間有些code
    
        isPassed = false
        return 
    }

    //聽說有指定變數名稱的回傳,速度上是比較快的。








Created Date : 2018/08/03

Last Updated Date : 2018/08/03

留言

這個網誌中的熱門文章

[Go] 型態轉換 type convert

[Go] Golang用法 package import 前面的底線

[Go] 指標 pointer with golang