在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

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)
}