I am new to Go. I have read that encapsulation in Go is on the package level. I have a simple web controller use case. I have a struct which comes in as a JSON object and is Unmarshaled into the struct type.
type User struct{
Name String `json:"name"`
//Other Variables
}
Now a json can be unmarshaled into type User Struct by json.Unmarshal([]byte). However, this User struct is available to other packages too. How do I make sure that only methods related to User are accessible by other packages.
One solution I could think of :
type User struct{
name String
}
type UserJSON struct{
Name String `json:"name"`
}
func DecodeJSONToUser(rawJSON []byte) (User,error) {
var userJSON UserJSON
err := json.Unmarshal(rawJSON,&userJSON)
//Do error handling
return User{name:userJSON.Name},nil
}
Is there a GOish way to achieve this ?