我想在Go中编写一个函数,它需要一个JSON与一个目录的URL并执行BFS来查找该目录中的文件。当我找到一个作为目录的JSON时,代码会生成一个URL并且应该排入该URL。当我尝试在循环中创建append()
中的结构时,出现错误。Golang不能用作类型结构数组或切片文字
type ContentResp []struct {
Name string `json:"name"`
ContentType string `json:"type"`
DownloadURL string `json:"download_url"`
}
...
var contentResp ContentResp
search(contentQuery, &contentResp)
for _, cont := range contentResp {
append(contentResp, ContentResp{Name:cont.Name, ContentType:"dir", DownloadURL:cont.contentDir.String()})
}
./bfs.go:129: undefined: Name
./bfs.go:129: cannot use cont.Name (type string) as type struct { Name string "json:\"name\""; ContentType string "json:\"type\""; DownloadURL string "json:\"download_url\"" } in array or slice literal
./bfs.go:129: undefined: ContentType
./bfs.go:129: cannot use "dir" (type string) as type struct { Name string "json:\"name\""; ContentType string "json:\"type\""; DownloadURL string "json:\"download_url\"" } in array or slice literal
./bfs.go:129: undefined: DownloadURL
./bfs.go:129: cannot use cont.contentDir.String() (type string) as type struct { Name string "json:\"name\""; ContentType string "json:\"type\""; DownloadURL string "json:\"download_url\"" } in array or slice literal