我只是尝试创建以下代码
package main
import (
"fmt"
)
type Image struct {
filename string
format string
count int
}
type Site struct {
url string
images map[string]Image
}
func main(){
gambars := map[string]Image{
"http://example.com/img.jpg": {"img.jpg", "jpg", 1},
"http://example.com/img2.jpg": {"img2.jpg", "jpg", 1},
}
site := Site{url: "http://example.com", images: gambars}
site.images["http://example.com/img.jpg"].count += 1
fmt.Println(site.images["http://example.com/img.jpg"].count)
}
出现以下错误
cannot assign to struct field site.images["http://example.com/img.jpg"].count in map
咨询了我的拼贴画后,他建议我将Image实例设置为指针,并在重新运行代码后按预期进行,所以更改如下所述
...
images map[string]*Image
...
gambars := map[string]*Image{
但是当我要求解释时,他也不知道(这确实有效),有人可以帮助我进行解释吗?