我想在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