go chapter 10 函数 方法 struct的方法

时间:2023-03-10 03:33:53
go chapter 10  函数  方法  struct的方法

1. struct的方法

// 定义struct
type MyStruct struct{}
// 定义方法 (那个对象可以回调)方法名(参数) 返回值 {}
(s *MyStruct) FillStruct(m map[string]interface{}) error {}
// 初始化对象(不是类型), 调用方法
result := &MyStruct{}
err := result.FillStruct(myData)

  

package entities

type TestObj struct {
Id string
Name string
} func (r *TestObj) GetTestStructural(a string) (string) {
return "123-" + a + "-" + r.Id
}
//==================
func TestS1() {
r := entities.TestObj{Name:"tom", Id:"jerry"}
b := r.GetTestStructural("hehe")
fmt.Println(b) // 123-hehe-jerry
}