Menu
Coddy logo textTech

Exported vs Unexported Names

Part of the Object Oriented Programming section of Coddy's GO journey — lesson 4 of 107.

In Go, the first letter of a name determines its visibility. Names starting with a capital letter are exported (accessible from other packages). Names starting with a lowercase letter are unexported (private to the package).

Exported names — visible outside the package

type User struct {
    Name  string  // Exported field
    Email string  // Exported field
}

func CreateUser(name string) User { // Exported function
    return User{Name: name}
}

Unexported names — only visible inside the package

type secret struct {     // Unexported struct
    password string      // Unexported field
}

func validate() bool {   // Unexported function
    return true
}

Mixed — exported struct with some unexported fields

type Account struct {
    Username string    // Exported - accessible anywhere
    Email    string    // Exported - accessible anywhere
    password string    // Unexported - only inside the package
}

This naming convention replaces access modifiers like public and private found in other languages.

// Capital letter   = Exported (public)
// Lowercase letter = Unexported (private)

func SaveData() {}  // Exported
func loadData() {}  // Unexported

Following this convention is essential in Go. When other packages import your code, they can only access names that start with a capital letter.

challenge icon

Challenge

Medium

Fix all the names in product.go to follow Go's exported naming convention. Capitalize the first letter of:

  • The struct name: productProduct
  • All field names: name, price, stock
  • All function names: totalValue, displayInfo
  • All field references inside the functions

Cheat sheet

In Go, the first letter of a name determines its visibility:

  • Capital letter = Exported (accessible from other packages)
  • Lowercase letter = Unexported (private to the package)

Exported names:

type User struct {
    Name  string  // Exported field
    Email string  // Exported field
}

func CreateUser(name string) User { // Exported function
    return User{Name: name}
}

Unexported names:

type secret struct {     // Unexported struct
    password string      // Unexported field
}

func validate() bool {   // Unexported function
    return true
}

Mixed visibility:

type Account struct {
    Username string    // Exported - accessible anywhere
    Email    string    // Exported - accessible anywhere
    password string    // Unexported - only inside the package
}

This naming convention replaces access modifiers like public and private found in other languages.

Try it yourself

package main

import "fmt"

func main() {
    var name string
    var price float64
    var stock int
    fmt.Scan(&name, &price, &stock)

    p := Product{
        Name:  name,
        Price: price,
        Stock: stock,
    }

    fmt.Println(DisplayInfo(p))
    fmt.Printf("Total Value: %.2f\n", TotalValue(p))
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming