我正在尝试在Go中使用数组,但找不到在同一数组中可用于整数和字符串的任何东西。我正在寻找一些有助于解决此问题的文档。
我有它在Python中工作,我正在尝试将其翻译成Go。我在网上找到的大多数信息都显示整数数组或字符串数组,但未同时显示两者。
整数和字符串将被传递到另一个函数中,具体取决于整数的值,确定将哪个字符串连接到数组的字符串值。
这是来自Python的示例:
# This is the set arrays
List = [[1, "Pie"], [10, "Fish"], [5, "apples"]]
#This is the code of the function that each array will be passed into
if list[0] == 1:
return "There is one " + list[1] + "."
else:
return "There are " + str(list[0]) + " " + list[1] + "."
Run Code Online (Sandbox Code Playgroud)
最终打印输出:
# This is the set arrays
List = [[1, "Pie"], [10, "Fish"], [5, "apples"]]
#This is the code of the function that each array will be passed into
if list[0] == 1:
return "There is one " + list[1] + "."
else:
return "There are " + str(list[0]) + " " + list[1] + "."
Run Code Online (Sandbox Code Playgroud)
我建议像这样去做
type Foo struct {
Number int
Text string
}
// ...
array := []Foo{{Number: 1, Text: "pie"}, {Number: 10, Text: "fish"}, {Number: 5, Text: "apples"}}
if array[0].Number == 1 {
fmt.Println(array[0].Text)
}
// ...
Run Code Online (Sandbox Code Playgroud)