我想知道为什么以下代码中没有警告或错误允许我覆盖全局变量。
// You can edit this code!
// Click here and start typing.
package main
import "fmt"
type MyStruct struct {
MyInt uint32
}
func (s *MyStruct) MyMethod() {
fmt.Println(s.MyInt)
}
var theStruct MyStruct
func main() {
// Override the above global variable
// I would expect some kind of warning or error here?
theStruct := MyStruct {
MyInt: 42,
}
// Outputs 42
theStruct.MyMethod()
// Outputs 0
UseMyStruct()
}
func UseMyStruct() {
theStruct.MyMethod()
}
Smart猫小萌
相关分类