SOLVED: Golang access variable from another package

In this article, we shall discuss step by step how to access variables from another package in Go with practical examples.


Brief overview on golang variables

What is a variable in Go

In Go, A variable is a name given to a location in memory space where data is stored. This variable can be of different data type namely:- int, string, bool, float associated with it. Its syntax is var varName varType or varName := varTypeValue



How to declare and use variable

We can declare variables using two methods:-

  • Using var keyword i.e var varName varType for example var firstName string
  • Using shorthand method :=  i.e varName := varTypeValue for example firstName := "John"

The example below shows two ways of creating a variable:-

package main

import "fmt"

func main() {
	// declaration using var keyword
	var fullName string
	// assigning value
	fullName = "John Doe"
	// usage
	fmt.Printf("Using var Keyword: My fullName is %s \n", fullName)

	// declaration and assigning value using shorthand:=
	companyName := "goLinuxCloud"
	// usage
	fmt.Printf("Using shorthand := : My companyName is %s", companyName)

Output:- Both sample output is the same

$ go run main.go
Using var Keyword: My fullName is John Doe 
Using shorthand := : My companyName is goLinuxCloud

Explanation:- We just wrote a simple program to demonstrate how to declare and assign variable values using different methods.We first define our package name e.g package main then imported required package i.e import "fmt" package which helps us with the fmt.Printf() function for formatting standard output in Golang.

We have already written a dedicated article explaining the difference between usage of var and := while declaring a variable.


Brief overview on golang packages

What is a package in Go

In Go, we simply define package; as a collection of source files within the same working directory that are compiled together. It always comprises variables, functions, constants, and various data types which are accessible by other sources within the same package. Examples of Go packages fmt, os etc.



How to create a package in Go

Kindly learn more about modules in Go in our previous article using this link modules in Go

Here for the sake of this article I have created my go environment with below structure of one package:

$ mkdir -p goexamples/global-vars
$ cd goexamples/global-vars
$ touch main.go
$ mkdir util
$ touch util/common.go
$ go mod init global-vars
go: creating new go.mod: module global-vars
go: to add module requirements and sums:
        go mod tidy
$ go mod tidy
$ cat go.mod 
module global-vars

go 1.17
$ cd ..
$ tree .
└── global-vars
    ├── go.mod
    ├── main.go
    └── util
        └── common.go

2 directories, 3 file

So now we will try to access variable from common package into main package


Access variable from another package

In Golang, we can declare a global variable that is accessible across the packages. The syntax of a global variable is that the first letter of that variable must start with a capital letter or uppercase naming convention.


For example, here is the content of util/common.go from my go environment. As you can see I have defined my global variable with First letter as UPPERCASE or CAPITAL LETTERS i.e. <strong>M</strong>ypath. Now you are free to decide the case of other letters in the variables.

package util

var Mypath string

func init() {
	Mypath = "/tmp"


Now to access this global variable from main.go, we have to refer the variable based on it's package name. Since the package name where Mypath is defined is "util" so to access this variable inside main.go we will use util.Mypath as you can see below:

package main

import (

func main() {
	fmt.Println("The path is: " + util.Mypath)


$ go run main.go 
The path is: /tmp


Can I access variables from main to another sub-package in golang?

NO, you can not access variables defined in main package into any other child or sub-package of your application as Go does not allow import loops.

In such case it is recommended to define a global variable in your sub-package and then import that package into main to access the variable. You can only access variables from another packages to main and not the other way around.



This article covered what is a variable, how to declare and use a variable, what is a package, and how to write definitions within a package demonstrated as well as make use of those definitions within another Go programming file, and explained the options for where to keep the package in order to access it.

Golang variable scope has been so important in the new technology spectrum. For example, terraform used for cloud computing and automation of application deployment uses global variables to access those values. This gives Golang an advantage in cutting-edge technology. Other tools like Docker, and Kubernetes use the same mechanism.



Variable scope



Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment