1. 首页
  2. 主题
  3. Go问与答

获取不到map中struct对象的指针

__Golang__ · · 1308 次点击
请教为啥无法获取`stus["a"]`的指针 ``` type Student struct { Name string } func main() { stus := make(map[string]Student) stus["a"] = Student{"a"} p := &stus["a"] //cannot take the address of stus["a"] } ```
应该就是`每一个字都看得懂,连起来就不懂了`
#6
更多评论
这是因为stus["a"]的返回值不是固定的地址, 算不出来吧, 因为每次都要复制一次. 你如果把stus["a"]赋值给一个栈变量就可以了. ```go s := stus["a"] //p := stus["a"] //cannot take the address of stus["a"] p := &s // 这样就不会报错了 ```
#1

用户登录

没有账号?注册

今日阅读排行

    加载中

一周阅读排行

    加载中