在Go中同时使用int和str数组
我试图在Go中使用数组,但在同一数组中找不到任何同时适用于整数和字符串的数组。我正在寻找一些可以帮助我解决这个问题的文档 我用Python编写了它,我正在尝试将它翻译成Go。 我在网上找到的大多数信息要么显示整数数组,要么显示字符串数组,但不是两者的组合 integer&string将被传递到另一个函数中,具体取决于integer的值,该值决定将哪个字符串连接到数组的字符串值 这是Python中的一个示例:在Go中同时使用int和str数组,go,tuples,Go,Tuples,我试图在Go中使用数组,但在同一数组中找不到任何同时适用于整数和字符串的数组。我正在寻找一些可以帮助我解决这个问题的文档 我用Python编写了它,我正在尝试将它翻译成Go。 我在网上找到的大多数信息要么显示整数数组,要么显示字符串数组,但不是两者的组合 integer&string将被传递到另一个函数中,具体取决于integer的值,该值决定将哪个字符串连接到数组的字符串值 这是Python中的一个示例: # This is the set arrays List = [[1, "Pie"],
# 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] + "."
最终打印输出:
有一个馅饼。
有10条鱼。
有5个苹果。
如果确实希望仅对阵列执行此操作,请执行以下操作:
List:=[][]interface{}{ {1,"Pie"}, {10,"Fish"}, {5, "apples"} }
然后,您可以执行类型断言:
intValue:=List[0][0].(int)
strValue:=List[0][1].(string)
但是,更好的方法是定义一个包含int和string成员的结构,并使用该结构的数组。如果您真的只想使用数组执行此操作:
List:=[][]interface{}{ {1,"Pie"}, {10,"Fish"}, {5, "apples"} }
然后,您可以执行类型断言:
intValue:=List[0][0].(int)
strValue:=List[0][1].(string)
但是,更好的方法是定义一个包含int和string成员的结构,并使用该结构的数组。我建议这样做
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)
}
// ...
我建议这样做
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)
}
// ...
使用上面的解决方案,我能够得到我所需要的基本知识
// The function determines if it's one of or more than 1
// Giving the correct English sentence
import (
"fmt"
"strconv"
)
type Foo struct {
Number int
Text string
}
func main() {
// change the value of fish between 1 and 10 to test the function works as expected
// List could be pulled in from somewhere else then ran through this function
array := []Foo{{Number: 1, Text: "pie"}, {Number: 10, Text: "fish"}, {Number: 5, Text: "apples"}}
for i := 0; i <= 2; i++ {
n := strconv.Itoa(array[i].Number)
g := (array[i].Text)
if array[i].Number == 1 {
fmt.Println("There is " + n + " " + g + ".")
} else {
fmt.Println("There are " + n + " " + g + ".")
}
}
}
使用上面的解决方案,我能够得到我所需要的基本知识
// The function determines if it's one of or more than 1
// Giving the correct English sentence
import (
"fmt"
"strconv"
)
type Foo struct {
Number int
Text string
}
func main() {
// change the value of fish between 1 and 10 to test the function works as expected
// List could be pulled in from somewhere else then ran through this function
array := []Foo{{Number: 1, Text: "pie"}, {Number: 10, Text: "fish"}, {Number: 5, Text: "apples"}}
for i := 0; i <= 2; i++ {
n := strconv.Itoa(array[i].Number)
g := (array[i].Text)
if array[i].Number == 1 {
fmt.Println("There is " + n + " " + g + ".")
} else {
fmt.Println("There are " + n + " " + g + ".")
}
}
}
围棋之旅可能是一个有用的资源。如果排序无关紧要,请使用map[int]字符串围棋之旅可能是一个有用的资源。如果排序无关紧要,请使用map[int]字符串