ProgramGuru

Exported and Unexported Identifiers

Start with Exported and Unexported Identifiers

Go uses the first letter of an identifier to control access from other packages instead of separate public or private keywords.

What you’ll learn

  • Explain the main purpose of exported and unexported identifiers.
  • Read and run the complete Go example in this lesson.
  • Apply the concept and recognize common mistakes.

Understand the idea

An identifier beginning with an uppercase Unicode letter is exported from its package. Lowercase identifiers are accessible inside the same package but cannot be referenced directly by an importer. These rules apply to functions, types, variables, constants, methods and struct fields.

Names still matter inside a package: an exported struct type may contain an unexported field. Design exported APIs intentionally because their names become contracts callers rely on.

Visualize how it works

Follow this diagram before executing the example. Check where a value or decision changes what runs next.

Visual explanation for exportsCapitalization controls cross-package access.Account.OwnerStep 1Exported fieldStep 2Account.pinStep 3Package-private fieldStep 4Capitalization controls cross-package access.
Capitalization controls cross-package access.

Run a complete Go example

This example is a complete program. Save it as main.go or paste it into the Go Playground.

Go code

package main

import "fmt"

type Account struct {
	Owner string
	pin   int
}

func (a Account) Masked() string { return a.Owner + ": ****" }
func main() {
	a := Account{Owner: "Maya", pin: 1234}
	fmt.Println(a.Masked())
}

Output

Maya: ****

Trace what the program does

Inside the defining package, both Owner and pin can be initialized. Masked returns an exported method; an importing package can use Owner and Masked but cannot directly access the lowercase pin field.

Avoid common mistakes

  • An uppercase field of an exported type is exposed as part of its package API.
  • Exported does not mean globally mutable or automatically safe to use concurrently.

Try it yourself

Create an exported function Describe that reads a private field and returns a user-friendly description.

Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.

Check your understanding

Can another package access Account.pin directly?
Choose one answer. You can try again.

Answer and explanation

No, the name begins with lowercase p

Correct. Inside the defining package, both Owner and pin can be initialized.

What you should remember

You have practiced Exported and Unexported Identifiers in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.

Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Organizing Code Across Files →