Table of Contents
Introduction
In Go programming, identifiers play a crucial role in naming variables, functions, types, and packages. Go uses the concept of public and private identifiers to control access and visibility within the codebase. In this tutorial, we will explore the usage of public and private identifiers in Go and how they can be effectively utilized to write clean and maintainable code.
Before we proceed, it is assumed that you have a basic understanding of Go programming language and have Go installed on your system.
Public Identifiers
Public identifiers in Go are declared with an uppercase letter at the beginning of the identifier name. They are accessible from anywhere within the same package or from other packages.
Let’s create a simple example to illustrate the usage of public identifiers. Create a new file named public.go
and add the following code:
package main
import "fmt"
func PublicFunc() {
fmt.Println("This is a public function.")
}
const PublicConst = 42
type PublicStruct struct {
Name string
Age int
}
In the code above, we have defined a public function PublicFunc()
, a public constant PublicConst
, and a public struct PublicStruct
. These identifiers can be accessed from any package that imports the main
package.
To use the public identifiers in another package, create a new file named main.go
and add the following code:
package main
import (
"fmt"
"your-package-name"
)
func main() {
your_package_name.PublicFunc()
fmt.Println(your_package_name.PublicConst)
var s your_package_name.PublicStruct
s.Name = "John"
s.Age = 30
fmt.Println(s)
}
Replace your-package-name
with the actual package name you have chosen. Now, when you run the main.go
file, you will see the output:
This is a public function.
42
{John 30}
Here, we are able to access the public function, constant, and struct from the your-package-name
package.
Private Identifiers
Private identifiers in Go are declared with a lowercase letter at the beginning of the identifier name. They have restricted visibility and can only be accessed within the same package.
Let’s create a new file named private.go
and add the following code:
package main
import "fmt"
func privateFunc() {
fmt.Println("This is a private function.")
}
const privateConst = 42
type privateStruct struct {
name string
age int
}
In the code above, we have defined a private function privateFunc()
, a private constant privateConst
, and a private struct privateStruct
. These identifiers can only be accessed from within the same package (main
package in this case).
To demonstrate the usage of private identifiers, modify the public.go
file from the previous example as follows:
package main
import "fmt"
func PublicFunc() {
fmt.Println("This is a public function.")
privateFunc()
}
const PublicConst = 42
type PublicStruct struct {
Name string
Age int
privateStruct
}
Here, we have modified the PublicFunc()
to also call the privateFunc()
. We have also embedded the privateStruct
inside the PublicStruct
.
If you try to compile public.go
now, it will result in an error: undefined: privateFunc
.
This error occurs because the privateFunc
is not accessible outside of the private.go
file due to its private nature. Similarly, the privateStruct
cannot be accessed outside of the package.
Note: In Go, the visibility is determined at the package level, not at the file level. So, identifiers defined within the same package can be accessed irrespective of the file they are defined in, given their visibility is public.
Conclusion
In this tutorial, we have explored the concept of public and private identifiers in Go. Public identifiers, declared with an uppercase letter, are accessible from anywhere within the same package or from other packages. On the other hand, private identifiers, declared with a lowercase letter, have restricted visibility and can only be accessed within the same package. Understanding the usage and limitations of public and private identifiers is crucial for writing maintainable and reusable code in Go.
We’ve covered the basic usage of public and private identifiers, and you should now have a clear understanding of how to declare and use them in your Go projects. Feel free to experiment with different scenarios and explore the capabilities of public and private identifiers in Go. Happy coding!