结构体的私有属性:
type Order struct{ userId string Amount int } //提供一个外部的接口来操作类的属性 注意这里*Order func (this *Order) Read()string{ fmt.Println(this.userId) return this.userId } func (this *Order) SetUserId(userId string){ this.userId=userId }
测试:
func TestAdd(t *testing.T){ order:=inital.Order{Amount: 15} order.SetUserId("15") fmt.Println(order) order.Read() fmt.Println("this.userId=",order.Read()) }