在 Go 中,如何编写接受元组参数的函数?
In Go, how to write functions that take tuple arguments?
我是 Go 的新手,我正在将 Python 程序翻译成 Go。
我是三元运算符的忠实粉丝所以我很快就实现了
func t2(test bool, true_val, false_val string) string {
if test {
return true_val
} else {
return false_val
}
}
效果很好。
不幸的是我在 Python 中有这个:a = 'hi', 'hello' if xxx else 'bye', 'goodbye'
如何为字符串元组编写我的三元运算符?
我试过:
- 泛型但了解到它们在 Go 中不存在
- 执行
func t2(test bool, true_val, false_val (string, string)) (string, string)
但无法编译
- typedef:
type s2 string, string
和 func t2(test bool, true_val, false_val s2) s2
但它不编译
谢谢
使用 2 string
return 值实施
它可能看起来像这样:
func t(test bool, true1, true2, false1, false2 string) (string, string) {
if test {
return true1, true2
}
return false1, false2
}
正在测试:
a1, a2 := t(false, "hi", "hello", "bye", "goodbye")
fmt.Println(a1, a2)
a1, a2 = t(true, "hi", "hello", "bye", "goodbye")
fmt.Println(a1, a2)
输出(在 Go Playground 上尝试):
bye goodbye
hi hello
用切片实现 []string
return 值
如果我们用 string
个切片来实现它可能更容易阅读和使用:[]string
.
func t(test bool, trueVal []string, falseVal []string) []string {
if test {
return trueVal
}
return falseVal
}
正在测试:
trueVal := []string{"hi", "hello"}
falseVal := []string{"bye", "goodbye"}
a := t(false, trueVal, falseVal)
fmt.Println(a)
a = t(true, trueVal, falseVal)
fmt.Println(a)
输出(在 Go Playground 上尝试):
[bye goodbye]
[hi hello]
使用包装器实现 struct
return 值
您也可以选择创建包装器 struct
来保存任意数量的值(甚至具有任意/不同类型):
type Pair struct {
v1, v2 string
}
func t(test bool, trueVal Pair, falseVal Pair) Pair {
if test {
return trueVal
}
return falseVal
}
正在测试:
trueVal := Pair{"hi", "hello"}
falseVal := Pair{"bye", "goodbye"}
a := t(false, trueVal, falseVal)
fmt.Println(a)
a = t(true, trueVal, falseVal)
fmt.Println(a)
输出(在 Go Playground 上尝试):
{bye goodbye}
{hi hello}
您可以使用数组(如果数字可变,甚至可以使用切片):
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]
}
测试:
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)
}
我是 Go 的新手,我正在将 Python 程序翻译成 Go。
我是三元运算符的忠实粉丝所以我很快就实现了
func t2(test bool, true_val, false_val string) string {
if test {
return true_val
} else {
return false_val
}
}
效果很好。
不幸的是我在 Python 中有这个:a = 'hi', 'hello' if xxx else 'bye', 'goodbye'
如何为字符串元组编写我的三元运算符?
我试过:
- 泛型但了解到它们在 Go 中不存在
- 执行
func t2(test bool, true_val, false_val (string, string)) (string, string)
但无法编译 - typedef:
type s2 string, string
和func t2(test bool, true_val, false_val s2) s2
但它不编译
谢谢
使用 2 string
return 值实施
它可能看起来像这样:
func t(test bool, true1, true2, false1, false2 string) (string, string) {
if test {
return true1, true2
}
return false1, false2
}
正在测试:
a1, a2 := t(false, "hi", "hello", "bye", "goodbye")
fmt.Println(a1, a2)
a1, a2 = t(true, "hi", "hello", "bye", "goodbye")
fmt.Println(a1, a2)
输出(在 Go Playground 上尝试):
bye goodbye
hi hello
用切片实现 []string
return 值
如果我们用 string
个切片来实现它可能更容易阅读和使用:[]string
.
func t(test bool, trueVal []string, falseVal []string) []string {
if test {
return trueVal
}
return falseVal
}
正在测试:
trueVal := []string{"hi", "hello"}
falseVal := []string{"bye", "goodbye"}
a := t(false, trueVal, falseVal)
fmt.Println(a)
a = t(true, trueVal, falseVal)
fmt.Println(a)
输出(在 Go Playground 上尝试):
[bye goodbye]
[hi hello]
使用包装器实现 struct
return 值
您也可以选择创建包装器 struct
来保存任意数量的值(甚至具有任意/不同类型):
type Pair struct {
v1, v2 string
}
func t(test bool, trueVal Pair, falseVal Pair) Pair {
if test {
return trueVal
}
return falseVal
}
正在测试:
trueVal := Pair{"hi", "hello"}
falseVal := Pair{"bye", "goodbye"}
a := t(false, trueVal, falseVal)
fmt.Println(a)
a = t(true, trueVal, falseVal)
fmt.Println(a)
输出(在 Go Playground 上尝试):
{bye goodbye}
{hi hello}
您可以使用数组(如果数字可变,甚至可以使用切片):
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]
}
测试:
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)
}