分配结构体的切片

huangapple go评论94阅读模式
英文:

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 &lt; 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 &lt; 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 &lt; len(s)-1; i++ {
		if s[i] &lt; s[i+1] {
			s[i], s[i+1] = s[i+1], s[i]
		}
	}
	fmt.Println(s)
}

huangapple
  • 本文由 发表于 2022年7月17日 00:02:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/73005722.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定