我正在尝试解除对频道的引用,并使用该频道中的有效载荷来检查地图中是否有条目。
如果地图不是类型,它可以工作,但是如果地图具有类型,我总是会收到错误,提示类型<map>
不是表达式。
type writechan chan UserProfile
type usersmap map[string]UserProfile
var myuserprofile UserProfile
myuserprofile = <-myuserchannel
var username = myuserprofile.name
if _,ok := usersmap[username]; ok {
fmt.Println("Already registered ")
} else {
usersmap[username] = myuserprofile
}
错误消息在if行中显示类型usersmap不是表达式。如果我不使用类型usermap而是普通映射,则说map [string] string可以正常工作。