Search code examples
gostructvar

Difference between var Foo struct and type Foo struct


I have hard time to understand what is the diffrence between:

var requestPayLoad struct {
        Email string `json:"email"`
        Password string `json:"string"`
    }

and:

type jwtUSer struct {
    ID        int    `json:"id"`
    FirstName string `json:"first_name"`
    LastName  string `json:"last_name"`
}

one is type and one is a var.


Solution

    • var v T creates a variable, binds the identifier v to it, gives it the type T, and then initializes it to the zero-value of T.
    • type t T binds the identifier t to type T.

    In both cases type T may be a named or an unnamed (anonymous) type.