Golang 不能使用 as type struct array 或 slice literal
Golang cannot use as type struct array or slice literal
我正在尝试在 Go 中编写一个函数,该函数采用 JSON 和 URL 目录并执行 BFS 以在该目录中查找文件。当我找到作为目录的 JSON 时,代码生成 URL 并且应该将 URL 入队。当我尝试在循环中的 append()
中创建结构时,出现错误。
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
您的 ContentResp
类型是 切片 ,而不是结构,但是当您使用 composite literal 试图创建它的值:
type ContentResp []struct {
// ...
}
更准确地说,它是匿名结构类型的一部分。创建匿名结构的值是令人不快的,因此您应该创建(命名)一个仅是 struct
的类型,并使用其中的一部分,例如:
type ContentResp struct {
Name string `json:"name"`
ContentType string `json:"type"`
DownloadURL string `json:"download_url"`
}
var contentResps []ContentResp
其他问题:
让我们检查一下这个循环:
for _, cont := range contentResp {
append(contentResp, ...)
}
上面的代码遍及一个切片,并在其中尝试将元素附加到切片中。 2 个问题:append()
returns 必须存储的结果(它甚至可能必须分配一个新的、更大的后备数组并复制现有元素,在这种情况下结果切片将指向一个完全不同的数组,旧的应该被放弃)。所以应该这样使用:
contentResps = append(contentResps, ...)
第二:你不应该改变你正在测距的切片。 for ... range
计算范围表达式 一次 (最多),所以你改变它(向它添加元素)不会影响迭代器代码(它不会看到切片 header 更改)。
如果您遇到这样的情况,您有 "tasks" 待完成,但在执行期间可能会出现新任务(待完成,递归),通道是更好的解决方案。查看此答案以了解频道:What are golang channels used for?
我正在尝试在 Go 中编写一个函数,该函数采用 JSON 和 URL 目录并执行 BFS 以在该目录中查找文件。当我找到作为目录的 JSON 时,代码生成 URL 并且应该将 URL 入队。当我尝试在循环中的 append()
中创建结构时,出现错误。
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
您的 ContentResp
类型是 切片 ,而不是结构,但是当您使用 composite literal 试图创建它的值:
type ContentResp []struct {
// ...
}
更准确地说,它是匿名结构类型的一部分。创建匿名结构的值是令人不快的,因此您应该创建(命名)一个仅是 struct
的类型,并使用其中的一部分,例如:
type ContentResp struct {
Name string `json:"name"`
ContentType string `json:"type"`
DownloadURL string `json:"download_url"`
}
var contentResps []ContentResp
其他问题:
让我们检查一下这个循环:
for _, cont := range contentResp {
append(contentResp, ...)
}
上面的代码遍及一个切片,并在其中尝试将元素附加到切片中。 2 个问题:append()
returns 必须存储的结果(它甚至可能必须分配一个新的、更大的后备数组并复制现有元素,在这种情况下结果切片将指向一个完全不同的数组,旧的应该被放弃)。所以应该这样使用:
contentResps = append(contentResps, ...)
第二:你不应该改变你正在测距的切片。 for ... range
计算范围表达式 一次 (最多),所以你改变它(向它添加元素)不会影响迭代器代码(它不会看到切片 header 更改)。
如果您遇到这样的情况,您有 "tasks" 待完成,但在执行期间可能会出现新任务(待完成,递归),通道是更好的解决方案。查看此答案以了解频道:What are golang channels used for?