一个包中的私有属性   要用小写字母

package inital

import "fmt"

type Order struct{
   userId string
   Amount int
}

//
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())
}
Logo

欢迎加入 MCP 技术社区!与志同道合者携手前行,一同解锁 MCP 技术的无限可能!

更多推荐