英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论