在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

我是Go语言的新手,我正在将Python程序翻译为Go语言。 p>

我是三元运算符的忠实拥护者,所以我很快实现了 p>

  func t2(测试布尔值,true_val,false_val字符串)字符串{
如果测试{
 返回true_val 
}否则{
返回false_val 
} 
} 
  code>  pre> 
 
 

,效果很好。 p>

不幸的是, 我在Python中有这个: a ='hi','hello'if xxx else'bye','goodbye' code> p>

我的三元运算符将如何 p>

我尝试过: p>

  • 泛型,但得知它们在Go中不存在
  • 执行 func t2(测试布尔值,true_val,false_val(字符串,字符串))(字符串,字符串) code>,但无法编译 li>
  • typedef:类型s2字符串,字符串 code>和 func t2(测试布尔值,true_val,false_val s2)s2 code>,但无法编译 li> ul >

    谢谢 p> div>

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}

You can use an array (or even a slice if it the number is variable):

func iff(test bool, true_val, false_val [2]string) (string, string) {
    if test {
        return true_val[0], true_val[1]
    }
    return false_val[0], false_val[1]
}

test:

func main() {
    a, b := iff(false, [2]string{"hi", "hello"}, [2]string{"bye", "goodbye"})
    fmt.Println(a, b)

    a, b = iff(true, [2]string{"hi", "hello"}, [2]string{"bye", "goodbye"})
    fmt.Println(a, b)
}