It would be easier to do:

type User struct {
Name string `json:"name"`
}

type PrivateUser struct {
User
Password string `json:"password"`
}

See https://play.golang.org/p/2VlGgRlrP1

On Wednesday, November 30, 2016 at 2:42:19 PM UTC+5:30, Mirco Zeiss wrote:
>
> Maybe I'll just wait for go 1.8 where the following is possible, because 
> tags aren't part of types anymore.
>
> type PublicUser struct{
>   Password string `json:"-"`
>   Name string `json:"name"`
> }
>
> type PrivateUser struct{
>   Password string `json:"password"`
>   Name string `json:"name"`
> }
>
> func main() {
>   // get user from db
>   priv := PrivateUser{
>     Password: "secret",
>     Name: "john"
>   }
>   pub := PublicUser(priv)
>   // send pub to frontend
>   // ...
> }
>
> I still have to duplicate all models with every field but at least the 
> compiler will warn me when I forget a field.
>
> On Monday, November 28, 2016 at 1:35:42 PM UTC+1, Mirco Zeiss wrote:
>>
>> Sorry, but it won't work. I'm using the same marshal method when 
>> communicating with my DB as when communicating to my frontend. How can I 
>> tell it to sometimes leave out the private fields and sometimes not?
>>
>> On Monday, November 28, 2016 at 6:43:26 AM UTC+1, Mirco Zeiss wrote:
>>>
>>> That might work. Thank you! Will try and report back.
>>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to