<a name="introduction"></a>
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>
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.
<a name="exported-identifiers"></a>
Exported functions are accessible from other packages, while unexported functions are only accessible within the package they are defined in.
Exported Function Example:
package main
func Greet(name string) string {
return "Hello, " + name
}Unexported Function Example:
package main
func greetUnexported(name string) string {
// ...
}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:
package main
var Greeting = "World"Unexported Variable Example:
package main
var greetingUnexported stringSimilar 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:
package main
type Person struct {
Name string
Age int
}Unexported Type Example:
package main
type person struct {
name string
age int
}<a name="unexported-identifiers"></a>
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:
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>
<a name="practical-examples"></a>
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.
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.