0

I have the following two structs:

type Profile struct {
    Email        string   `json:"email"`
    Username     string   `json:"username"`
    Name         string   `json:"name"`
    Permissions  []string `json:"permissions"`
}

type Session struct {
    Token  string   `json:"token"`
    User   Profile  `json:"user"`
}

and I'm trying to create a new Session using:

session := Session{token, profile}

where token is a string and profile is a Profile both created earlier.

I'm getting the error cannot use profile (type *Profile) as type Profile in field value when I compile.

Am I missing something?

1 Answer 1

4

Your profile is a pointer. Either redefine your Session to be

type Session struct {
    Token  string    `json:"token"`
    User   *Profile  `json:"user"`
}

or dereference it.

session := Session{token, *profile}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks - what does derefencing actually mean then?
When you dereference a pointer, you get the value the pointer (which is an address) was pointing to. The type *Profile is an address of a Profile value. *p, where p is of type *Profile, will get the value.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.