[Go] Method

// User
type User struct {
	ID        int
	FirstName string
}

// Method
func (u *User) describe() string {
	desc := fmt.Sprintf("Name %s", u.FirstName)
	return desc
}

// Order:
// 0 1 2 defer panic: NOT 2
func main() {
	user := User{FirstName: "JK", ID: 1}
	fmt.Println(user.describe())
}

describe() is a prop method on User struct

posted @ 2022-09-06 02:31  Zhentiw  阅读(13)  评论(0编辑  收藏  举报