Skip to main content
deleted 5 characters in body
Source Link
Fenistil
  • 3.8k
  • 1
  • 29
  • 33

Simply include the UserData into the SecretUser struct and do not specify a json tag for it.

type UserInfo struct { FirstName string `json:"firstName"` LastName string `json:"lastName"` Email string `json:"email"` } type SecretUser struct { UserInfo Password string `json:"password"` } func main() { data := []byte(`{"firstName": "nice","lastName":"guy","email":"[email protected]","password":"abc123"}`) var u SecretUser _ := json.Unmarshal(data, &u) fmt.Println(u) } 

Go Play Space example

Simply include the UserData into the SecretUser struct and do not specify a json tag for it.

type UserInfo struct { FirstName string `json:"firstName"` LastName string `json:"lastName"` Email string `json:"email"` } type SecretUser struct { UserInfo Password string `json:"password"` } func main() { data := []byte(`{"firstName": "nice","lastName":"guy","email":"[email protected]","password":"abc123"}`) var u SecretUser _ := json.Unmarshal(data, &u) fmt.Println(u) } 

Go Play Space example

Simply include the UserData into the SecretUser struct and do not specify a json tag for it.

type UserInfo struct { FirstName string `json:"firstName"` LastName string `json:"lastName"` Email string `json:"email"` } type SecretUser struct { UserInfo Password string `json:"password"` } func main() { data := []byte(`{"firstName": "nice","lastName":"guy","email":"[email protected]","password":"abc123"}`) var u SecretUser json.Unmarshal(data, &u) fmt.Println(u) } 

Go Play Space example

Source Link
Fenistil
  • 3.8k
  • 1
  • 29
  • 33

Simply include the UserData into the SecretUser struct and do not specify a json tag for it.

type UserInfo struct { FirstName string `json:"firstName"` LastName string `json:"lastName"` Email string `json:"email"` } type SecretUser struct { UserInfo Password string `json:"password"` } func main() { data := []byte(`{"firstName": "nice","lastName":"guy","email":"[email protected]","password":"abc123"}`) var u SecretUser _ := json.Unmarshal(data, &u) fmt.Println(u) } 

Go Play Space example