In Go, how to write functions that take tuple arguments?

huangapple go评论79阅读模式
英文:

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),但它无法编译通过。
  • 使用类型定义:type s2 string, stringfunc t2(test bool, true_val, false_val s2) s2,但它也无法编译通过。

谢谢。

英文:

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

答案1

得分: 2

实现具有两个string返回值的函数可以如下所示:

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)

输出结果如下:

bye goodbye
hi hello

使用切片[]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)

输出结果如下:

[bye goodbye]
[hi hello]

你还可以选择创建一个包装的结构体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)

输出结果如下:

{bye goodbye}
{hi hello}

你可以在Go Playground上尝试运行以上代码。

英文:

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}

答案2

得分: 0

你可以使用一个数组(或者如果数字是可变的,甚至可以使用切片):

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

测试结果:

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

huangapple
  • 本文由 发表于 2016年4月10日 21:08:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/36530175.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定