分配一片结构

我试图将切片数据表示为 json 格式,以便我可以使用此vs-code-extension

但是我在 for 循环的最后一行出现错误。

错误:未使用 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)

    }


尚方宝剑之说
浏览 100回答 1
1回答

慕尼黑8549860

对代码进行某些更改后,以下代码现在可以正常工作。但是我仍然无法从这个 vs-code-extension 中获得可视化效果。type ColumnStructure struct {&nbsp; &nbsp; Content string&nbsp; &nbsp; Tag&nbsp; &nbsp; &nbsp;string}type RowStructure struct {&nbsp; &nbsp; Column ColumnStructure}type format struct {&nbsp; &nbsp; Kind map[string]bool&nbsp; &nbsp; Text string&nbsp; &nbsp; Rows []RowStructure}func serialize(s []int) format {&nbsp; &nbsp; var formatted format&nbsp; &nbsp; for i := 0; i < len(s); i++ {&nbsp; &nbsp; &nbsp; &nbsp; var col_i = ColumnStructure{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Content: string(s[i]),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Tag:&nbsp; &nbsp; &nbsp;string(s[i]),&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; var row = RowStructure{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Column: col_i,&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; formatted.Rows = append(formatted.Rows, row)&nbsp; &nbsp; }&nbsp; &nbsp; return formatted}func main() {&nbsp; &nbsp; var s = []int{1, 12, 30, 4, 5}&nbsp; &nbsp; formatted_data := serialize(s)&nbsp; &nbsp; fmt.Println(formatted_data)&nbsp; &nbsp; for i := 0; i < len(s)-1; i++ {&nbsp; &nbsp; &nbsp; &nbsp; if s[i] < s[i+1] {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s[i], s[i+1] = s[i+1], s[i]&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println(s)}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go