英文:
Assigning a slice of struct
问题
我正在尝试将一个切片数据表示为 JSON 格式,以便我可以使用这个 vs-code-extension。
但是我在循环的最后一行遇到了错误。
错误:append(formatted.Rows, col_i) (类型为 []RowStructure 的值) 未被使用compilerUnusedExpr
请帮我找出我做错了什么,以及正确分配结构体切片的方法。
谢谢
type ColumnStructure struct{
Content string
Tag string
}
type RowStructure struct{
Column ColumnStructure
}
type format struct{
Kind map[string]bool
Text string
Rows []RowStructure
}
func serialize(s []int){
var formatted format
for i := 0; i < len(s); i++ {
var col_i = ColumnStructure{
Content: string(s[i]),
Tag: string(s[i]),
}
append(formatted.Rows,col_i)
}
}
func main() {
var s = []int{1, 12, 30, 4, 5}
formatted_data := serialize(s)
fmt.Println(formatted_data)
}
英文:
I am trying to represent a slice data as json format so that I can use this vs-code-extension.
But I am getting error in the last line of the for loop.
Error: append(formatted.Rows, col_i) (value of type []RowStructure) is not usedcompilerUnusedExpr
Please help me find out what am I doing wrong and what should be the correct way of assigning slice of structs.
Thankyou
type ColumnStructure struct{
Content string
Tag string
}
type RowStructure struct{
Column ColumnStructure
}
type format struct{
Kind map[string]bool
Text string
Rows []RowStructure
}
func serialize(s []int){
var formatted format
for i := 0; i < len(s); i++ {
var col_i = ColumnStructure{
Content: string(s[i]),
Tag: string(s[i]),
}
append(formatted.Rows,col_i)
}
}
func main() {
var s = []int{1, 12, 30, 4, 5}
formatted_data := serialize(s)
fmt.Println(formatted_data)
}
答案1
得分: 0
在对代码进行一些更改后,以下代码现在可以正常工作。但是我仍然无法从这个 VS Code 扩展中获取可视化效果。
type ColumnStructure struct {
Content string
Tag string
}
type RowStructure struct {
Column ColumnStructure
}
type format struct {
Kind map[string]bool
Text string
Rows []RowStructure
}
func serialize(s []int) format {
var formatted format
for i := 0; i < len(s); i++ {
var col_i = ColumnStructure{
Content: string(s[i]),
Tag: string(s[i]),
}
var row = RowStructure{
Column: col_i,
}
formatted.Rows = append(formatted.Rows, row)
}
return formatted
}
func main() {
var s = []int{1, 12, 30, 4, 5}
formatted_data := serialize(s)
fmt.Println(formatted_data)
for i := 0; i < len(s)-1; i++ {
if s[i] < s[i+1] {
s[i], s[i+1] = s[i+1], s[i]
}
}
fmt.Println(s)
}
英文:
After doing certain changes to the code, following code works fine now.
But I am still not able to get the visualizations from this vs-code-extension.
type ColumnStructure struct {
Content string
Tag string
}
type RowStructure struct {
Column ColumnStructure
}
type format struct {
Kind map[string]bool
Text string
Rows []RowStructure
}
func serialize(s []int) format {
var formatted format
for i := 0; i < len(s); i++ {
var col_i = ColumnStructure{
Content: string(s[i]),
Tag: string(s[i]),
}
var row = RowStructure{
Column: col_i,
}
formatted.Rows = append(formatted.Rows, row)
}
return formatted
}
func main() {
var s = []int{1, 12, 30, 4, 5}
formatted_data := serialize(s)
fmt.Println(formatted_data)
for i := 0; i < len(s)-1; i++ {
if s[i] < s[i+1] {
s[i], s[i+1] = s[i+1], s[i]
}
}
fmt.Println(s)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论