这段代码中为什么不能改 a 的username 赋值
赋值就会报错
要先断言才行哦
a.(*MyStruct).username = "this is a test"
a = MyStruct{}
b := a.(MyStruct)
b.username = "this"
fmt.Println(b)
因为你的a依然是interface{}类型啊,这种类型自然没有username这个字段
之所以你可以执行a = MyStruct{}
,是因为所有的struct都是interface{},所以可以赋值,但赋值不意味着改变了a的类型
golang是静态语言,你把a声明为interface,他不能再声明为struct
你要在MyStruct
的方法里面赋值,或者自己写一个构造函数。