Go Structs Exported vs. Unexported Fields (Private vs Public)

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

Struct fields whose names begin with an uppercase letter are exported. All other names are unexported.

type Account struct {
    UserID      int    // exported
    accessToken string // unexported
}

Unexported fields can only be accessed by code within the same package. As such, if you are ever accessing a field from a different package, its name needs to start with an uppercase letter.

package main

import "bank"

func main() {
    var x = &bank.Account{
        UserID: 1,          // this works fine
        accessToken: "one", // this does not work, since accessToken is unexported
    }
}

However, from within the bank package, you can access both UserId and accessToken without issue.

The package bank could be implemented like this:

package bank

type Account struct {
    UserID int
    accessToken string
}

func ProcessUser(u *Account) {    
    u.accessToken = doSomething(u) // ProcessUser() can access u.accessToken because 
                                   // it's defined in the same package
}


Got any Go Question?