dpfwhb7470 2016-04-10 13:08
浏览 313
已采纳

在Go中,如何编写带有元组参数的函数?

I'm new to Go and I am translating a Python program to Go.

I'm a big fan of ternary operator so I quickly implemented

func t2(test bool, true_val, false_val string) string {
    if test {
        return true_val
    } else {
        return false_val
    }
}

which works fine.

Unfortunately I had this in Python: a = 'hi', 'hello' if xxx else 'bye', 'goodbye'

How does my ternary operator would have to be written for tuples of strings?

I have tried:

  • generics but learnt they don't exist in Go
  • do func t2(test bool, true_val, false_val (string, string)) (string, string) but it doesn't compile
  • typedef: type s2 string, string and func t2(test bool, true_val, false_val s2) s2 but it doesn't compile

Thanks

  • 写回答

2条回答 默认 最新

  • dsfovbm931034814 2016-04-10 13:18
    关注

    Implementing with 2 string return values

    It could look something like this:

    func t(test bool, true1, true2, false1, false2 string) (string, string) {
        if test {
            return true1, true2
        }
        return false1, false2
    }
    

    Testing it:

    a1, a2 := t(false, "hi", "hello", "bye", "goodbye")
    fmt.Println(a1, a2)
    
    a1, a2 = t(true, "hi", "hello", "bye", "goodbye")
    fmt.Println(a1, a2)
    

    Output (try it on the Go Playground):

    bye goodbye
    hi hello
    

    Implementing with slice []string return value

    It might be easier to read and work with if we implement it with string slices: []string.

    func t(test bool, trueVal []string, falseVal []string) []string {
        if test {
            return trueVal
        }
        return falseVal
    }
    

    Testing it:

    trueVal := []string{"hi", "hello"}
    falseVal := []string{"bye", "goodbye"}
    
    a := t(false, trueVal, falseVal)
    fmt.Println(a)
    
    a = t(true, trueVal, falseVal)
    fmt.Println(a)
    

    Output (try it on the Go Playground):

    [bye goodbye]
    [hi hello]
    

    Implementing with a wrapper struct return value

    You may also choose to create a wrapper struct to hold an arbitrary number of values (even having arbitrary / different types):

    type Pair struct {
        v1, v2 string
    }
    
    func t(test bool, trueVal Pair, falseVal Pair) Pair {
        if test {
            return trueVal
        }
        return falseVal
    }
    

    Testing it:

    trueVal := Pair{"hi", "hello"}
    falseVal := Pair{"bye", "goodbye"}
    
    a := t(false, trueVal, falseVal)
    fmt.Println(a)
    
    a = t(true, trueVal, falseVal)
    fmt.Println(a)
    

    Output (try it on the Go Playground):

    {bye goodbye}
    {hi hello}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动