package main
import(
"fmt"
)
type person struct {
name string
age int
}
var P person
P.name = "annie"
P.age = 20
func main() {
fmt.Printf("The person's name is %s", P.name)
}
报错信息:/struct.go:14: syntax error: non-declaration statement outside function body
你非得在main函数外赋值的话,要这样写
package main
import(
"fmt"
)
type person struct {
name string
age int
}
var P =person{"annie",20}
func main() {
fmt.Printf("The person's name is %s", P.name)
}
要不然就得这样写
package main
import(
"fmt"
)
type person struct {
name string
age int
}
var P person
func main() {
P.name="a"
P.age=10
fmt.Printf("The person's name is %s", P.name)
}