Accessing Global Variables in Different Packages in Go

Hey! If you love Go and building Go apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

Want to learn how to build better Go applications faster and easier? You can.

Check out my course on the Go Standard Library. You can check it out now for free.


In this article, we will explore how to access global variables in different packages in Go. We will discuss the different ways of accessing global variables and provide examples to help you understand the concepts better.



In Go, a package is a collection of related code that performs a specific task or set of tasks. Packages can be used to organize code into reusable components, making it easier to maintain and evolve large codebases. However, packages are not isolated from each other, and they often need to share data between them.

Global variables are variables that are declared outside any function or type declaration. They are available throughout the package in which they are defined. In Go, global variables can be accessed using a simple variable name without any qualification. However, when accessing a global variable from another package, we need to use a qualified name to disambiguate it from other variables with the same name.

Accessing Global Variables in the Same Package

In Go, global variables can be accessed directly using their simple names without any qualification. For example:

package main

var x int = 10

func main() {
    fmt.Println(x) // Output: 10
}

In this example, we declare a global variable x with the value of 10 in package main. We can access it directly from within the package using its simple name without any qualification.

Accessing Global Variables Across Packages

When accessing a global variable from another package, we need to use a qualified name to disambiguate it from other variables with the same name. The qualified name consists of two parts: the package name and the simple name of the variable. For example:

package main

import "fmt"

var x int = 10

func main() {
    fmt.Println(main.x) // Output: 10
}

In this example, we access the global variable x from package main by using its qualified name main.x. The main in front of the dot represents the package name, and x is the simple name of the variable.

Using an Alias for a Package Name

If we need to access multiple global variables from the same package, it can become repetitive to use the qualified names over and over again. To make things easier, we can use an alias for the package name. For example:

package main

import "fmt"

var x int = 10
var y int = 20

func main() {
    m := main // Alias for main package
    fmt.Println(m.x, m.y) // Output: 10 20
}

In this example, we create an alias m for the package name main, and use it to access multiple global variables from the same package. The qualified names are shorter and easier to read now.

Using a Constant or Variable

Sometimes, we may want to avoid using global variables altogether and instead use constants or variables that are defined within our own package. For example:

package main

import "fmt"

const x = 10
var y int = 20

func main() {
    fmt.Println(x, y) // Output: 10 20
}

In this example, we define constants and variables within our own package using the const or var keywords. We can then access these constants and variables directly without any qualification. This approach is often preferred because it makes our code more self-contained and easier to read.

Conclusion

In this article, we have learned how to access global variables in different packages in Go. We have seen how to use qualified names, aliases, and constants or variables within our own package to make accessing global variables easier. Remember that global variables can be accessed directly using their simple names without any qualification from within the same package. However, when accessing a global variable from another package, we need to use a qualified name to disambiguate it from other variables with the same name.


Questions or comments? Reach out to me


Learn how to leverage the Go Standard Library like a PRO.

I just created a new course, The Go Standard Library, check it out!