编译器错误:将切片追加到切片

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

Compiler error appending slice to slice

问题

Go编译器在我的代码中将一个切片附加到另一个切片时发出了警告。以下是相关的代码片段:

type LanidEntry struct {
	lanid   string
	group   string
	contact string
}

var lanids []LanidEntry

func load_file() (lanids_loaded []LanidEntry, errormsgs string) {
	// ...
}

func Load() (lanids []LanidEntry, errormessages string) {
	lanids_loaded, errormsgs := load_file(filename1, contact1)
	lanids = append(lanids, lanids_loaded)
	// ...
}

append行生成了以下编译器消息:

 src\load_lanids\load_lanids.go:50: cannot use lanids_loaded (type []LanidEntry) as type LanidEntry in append

根据Go博客文章中标题为Append: The built-in function的部分中的示例,我知道将切片附加到切片是可以的。

英文:

The Go compiler is complaining about my code to append a slice to a slice. Here are relevant excerpts:

type LanidEntry struct {
	lanid   string
	group   string
	contact string
}

var lanids []LanidEntry

func load_file() (lanids_loaded []LanidEntry, errormsgs string) {
	// ...
}

func Load() (lanids []LanidEntry, errormessages string) {
	lanids_loaded, errormsgs := load_file(filename1, contact1)
	lanids = append(lanids, lanids_loaded)
	// ...
}

The append line generates this compiler message:

 src\load_lanids\load_lanids.go:50: cannot use lanids_loaded (type []LanidEntry) as type LanidEntry in append

I know that appending slices to slices works fine, based on an example in a Go Blog post
under the section headed Append: The built-in function.

答案1

得分: 4

你需要使用 ...

lanids = append(lanids, lanids_loaded...)

另外,请格式化你的代码 编译器错误:将切片追加到切片

你还应该阅读维基上的Slice Tricks

英文:

You need to use ...:

lanids = append(lanids, lanids_loaded...)

Also, also please format your code 编译器错误:将切片追加到切片

You should also read Slice Tricks on the Wiki.

huangapple
  • 本文由 发表于 2015年8月11日 22:38:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/31944825.html
匿名

发表评论

匿名网友

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

确定