英文:
Convert [][]interface to [][]string
问题
有没有简单的方法将[][] interface{}
转换为[][]string
?我想要将这个[][] interface{}
写入一个csv
文件,但是go
中的writer
只接受[][]string
类型。额外信息:我的[][] interface{}
包含4列,其中2列是字符串,2列是json.Number
类型。
提前感谢。
英文:
Is there any easy way to convert [][] interface{}
to [][]string
?
What I want is to to write this [][] interface{}
to a csv
but writer
in go
accepts only [][]string
.
Additional info : my [][] interface{}
contains 4 columns 2 of them are string and 2 are json.Number
.
Thanks in advance.
答案1
得分: 3
最简单的方法可能是创建新的切片并编写一些循环:
var orig [][]interface{}
var strs = make([][]string, len(orig))
for i := range orig {
strs[i] = make([]string, len(orig[i]))
for j := range orig[i]{
strs[i][j] = fmt.Sprint(orig[i][j])
}
}
英文:
Simplest way is probably to make new slices and write some loops:
var orig [][]interface{}
var strs = make([][]string, len(orig))
for i := range orig {
strs[i] = make([]string, len(orig[i]))
for j := range orig[i]{
strs[i][j] = fmt.Sprint(orig[i][j])
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论