Go Exported vs Unexported: A Comprehensive Guide 🎯

beginner
10 min

Go Exported vs Unexported: A Comprehensive Guide 🎯

Table of Contents

  1. Introduction
  2. Understanding Exported and Unexported Identifiers in Go
  3. Exported Identifiers: Functions, Variables, and Types
  4. Unexported Identifiers: Functions, Variables, and Types
  5. When to Use Exported and Unexported Identifiers
  6. Practical Examples
  7. Quiz

<a name="introduction"></a>

1. Introduction 📝

Welcome to our deep dive into the world of Go! Today, we're going to talk about a fundamental concept: Exported and Unexported identifiers. This knowledge will empower you to write cleaner, more efficient Go code. Let's get started!

<a name="exported-vs-unexported"></a>

2. Understanding Exported and Unexported Identifiers in Go 💡

In Go, every identifier (variable, function, or type) can either be exported or unexported. The difference between the two lies in their visibility and accessibility within your Go code.

  • Exported Identifiers (also known as exported names) have names beginning with an uppercase letter (A-Z or an underscore _) and can be accessed from other packages.
  • Unexported Identifiers (also known as private identifiers) have names beginning with a lowercase letter or an underscore and are only visible within the package they are defined in.

<a name="exported-identifiers"></a>

3. Exported Identifiers: Functions, Variables, and Types 📝

3.1 Functions

Exported functions are accessible from other packages, while unexported functions are only accessible within the package they are defined in.

Exported Function Example:

go
package main func Greet(name string) string { return "Hello, " + name }

Unexported Function Example:

go
package main func greetUnexported(name string) string { // ... }

3.2 Variables

Exported variables, when used in Go programs, can be accessed from other packages. On the other hand, unexported variables are only accessible within the package they are defined in.

Exported Variable Example:

go
package main var Greeting = "World"

Unexported Variable Example:

go
package main var greetingUnexported string

3.3 Types

Similar to functions and variables, exported types (structs, interfaces, and custom types) can be accessed from other packages, while unexported types are only visible within the package they are defined in.

Exported Type Example:

go
package main type Person struct { Name string Age int }

Unexported Type Example:

go
package main type person struct { name string age int }

<a name="unexported-identifiers"></a>

4. Unexported Identifiers: Functions, Variables, and Types 📝

As mentioned earlier, unexported identifiers are only visible within the package they are defined in. This makes them useful for implementing private methods, variables, and types within your Go code.

Unexported Function Example:

go
package main func greetUnexported(name string) string { return "Hello, " + name } func Greet(name string) string { // Call the unexported function greeting := greetUnexported("World") return "Hello, " + name + ". Your greeting is: " + greeting }

In the example above, the greetUnexported function is unexported, but we can still call it from an exported function (Greet).

<a name="when-to-use"></a>

5. When to Use Exported and Unexported Identifiers 💡

  • Use exported identifiers when you want to expose your functions, variables, or types to other packages.
  • Use unexported identifiers when you want to implement private methods, variables, or types within your package.

<a name="practical-examples"></a>

6. Practical Examples 🎯

6.1 Exported Struct Example:

go
package main type User struct { Name string Age int } func (u User) Greet() string { return "Hello, " + u.Name } func main() { user := User{Name: "Alice", Age: 25} greeting := user.Greet() fmt.Println(greeting) }

In this example, the User struct is exported, and we can define a method (Greet) on it. We can then create a User object and call the Greet method.

6.2 Unexported Struct Example:

go
package main type user struct { name string age int } func newUser(name string, age int) user { return user{name, age} } func (u user) Greet() string { return "Hello, " + u.name } func main() { user := newUser("Alice", 25) greeting := user.Greet() fmt.Println(greeting) } ``<a name="quiz"></a> ## 7. Quiz 💡 :::quiz Question: What is the difference between an exported and an unexported identifier in Go? A: Exported identifiers are only visible within the package they are defined in, while unexported identifiers can be accessed from other packages. B: Exported identifiers can only be accessed from other packages, while unexported identifiers are only accessible within the package they are defined in. C: Exported identifiers can be accessed from other packages and unexported identifiers cannot be accessed from other packages. Correct: B Explanation: In Go, exported identifiers can be accessed from other packages, while unexported identifiers are only visible within the package they are defined in. This makes unexported identifiers useful for implementing private methods, variables, and types within your Go code.