英文:
Convert interface{} into []string in Golang
问题
我是你的中文翻译助手,以下是翻译好的内容:
我是GO语言的新手,我正在尝试从一个返回两个字符串的函数中获取一个值,即[str1, str2]
。
ret, err := fun1(....)
函数的原型是:
func fun1(...) (interface{}, error)
我打印了ret
,结果是:
[[57 57 56 56] [97 100 98 49 57 55 102 98 49 101 57 48 52 98 102 56 55 102 52 56 57 57 55 54 49 55 101 57 49 100 98 49]]
看起来像是[][]uint8
类型,所以我尝试了以下方式:
v, _ := ret.([][]uint8)
str1, _ := strconv.Atoi(string(v[0]))
str2, _ := strconv.Atoi(string(v[1]))
但是失败了,v
是[]
。
我还尝试了以下方式:
v, _ := ret.([]string)
但是仍然没有改变。
如何解决这个问题?任何帮助将不胜感激
PS:我进行了调试,发现第一个字符串是"9988"
。
英文:
I am new to GO language and I am trying to get a value from a function which returns two strings, that is [str1, str2]
ret, err := fun1(....)
And function's prototype is
func fun1(...) (interface{}, error)
I print ret
, it is
[[57 57 56 56] [97 100 98 49 57 55 102 98 49 101 57 48 52 98 102 56 55 102 52 56 57 57 55 54 49 55 101 57 49 100 98 49]]
which looks like the type [][]uint8
, so, I tried this way
v, _ := ret.([][]uint8)
str1, _ := strconv.Atoi(string(v[0]))
str2, _ := strconv.Atoi(string(v[1]))
it failed, v is []
And I also tried:
v, _ := ret.([]string)
nothing changed
How to solve this?
Any help will be appreciated
PS: I debug and find the first string is "9988"
答案1
得分: 4
变量ret
是一个包含[]byte
元素的[]interface{}
。使用以下代码将其转换为字符串切片:
// 使用类型断言从ret获取[]interface{}
sitf, ok := ret.([]interface{})
if !ok { /* 在此处理意外类型 */ }
// 创建结果切片
s := make([]string, len(sitf))
// 对于每个元素...
for i := range sitf {
// 使用类型断言从interface{}获取[]byte
sbyte, ok := sitf[i].([]byte)
if !ok { /* 在此处理意外类型 */ }
// 将[]byte转换为字符串并设置到结果中
s[i] = string(sbyte)
}
我从你的其他问题中了解到你在询问关于Redigo的问题。你可以使用redis.Strings辅助函数来替代上面的代码:
s, err := redis.Strings(fun1(....))
变量s
是一个[]string
。
有关更多信息,请参阅关于回复辅助函数的文档。
英文:
The value ret
is an []interface{}
containing []byte
elements. Use the following code to convert to a slice of strings:
// Use type assertion to get []interface from ret
sitf, ok := ret.([]interface{})
if !ok { /* handle unexpected type here */ }
// Create the result slice.
s := make([]string, len(sitf))
// For each element …
for i := range sitf {
// Use type assertion to get []byte from interface{}
sbyte, ok := sitf[i].([]byte)
if !ok { /* handle unexpected type here */ }
// Convert the []byte to a string and set in result
s[i] = string(sbyte)
}
I know from your other questions that you are asking about Redigo. You can use the redis.Strings helper function instead of the code above:
s, err := redis.Strings(fun1(....))
The variabile s
is a []string.
See the documentation about reply helpers for more information.
答案2
得分: 0
你有一个切片的切片,你需要将每个切片转换成你想要的类型。假设它们是字节切片,并且你想要打印切片中的每个字符串,那么可以按照以下方式进行操作:
ret, err := fun1(....)
if err != nil {
panic(err)
}
for idx, val := range ret.([][]byte) {
fmt.Println("在索引", idx, "处的字符串为:", string(val))
}
以上是使用上述数据的示例:https://play.golang.org/p/VsZhSaGmQk
英文:
You have a slice of slices, you need to cast each slice into what you want. Let's say they're bytes and you want to print each string in the slice, then:
ret, err := fun1(....)
if err != nil {
panic(err)
}
for idx, val := range ret.([][]byte) {
fmt.Println("At idx", idx, "string:", string(val))
}
Example with the data above: https://play.golang.org/p/VsZhSaGmQk
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论