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
}

  

 

posted @ 2018-08-03 13:21  webglcn  阅读(144)  评论(0编辑  收藏  举报