DEV Community

Jeevachaithanyan Sivanandan
Jeevachaithanyan Sivanandan

Posted on

How to Import Packages in Go

#go

When a Go project grows larger, it becomes necessary to access functions and variables from different packages. In Go, we can do this using the import statement.

Project Structure Example

Consider we have a project structure as below:

MainFolder/
│── FolderA/
│── FolderB/
│── FolderC/
│── main.go
│── go.mod
│── go.sum

FolderA has a function → FunctionA

FolderB has a function → FunctionB

FolderC has a function → FunctionC

Enter fullscreen mode Exit fullscreen mode

Importing a Package in main.go

If we need to use FunctionA inside main.go, we import the package as follows:

package main

import (
    "MainFolder/FolderA" // Importing FolderA package
)

func main() {
    // Now we can access functions from FolderA
    FolderA.SampleFunction2() // ✅ This works because it starts with an uppercase letter
    FolderA.sampleFunction1() // ❌ Error: Unexported function (starts with lowercase)
}
Enter fullscreen mode Exit fullscreen mode

This rule also applies to struct fields:

type User struct {
    Name  string  // Exported
    age   int     // Unexported
}

Enter fullscreen mode Exit fullscreen mode

*Exported vs Unexported Functions
*

Functions that start with an uppercase letter (SampleFunction2()) are exported and can be accessed from other packages.

Functions that start with a lowercase letter (sampleFunction1()) are unexported and can only be accessed within the same package.

*Example: Correct Function Declarations in FolderA/a.go
*

package FolderA // Package name should match the folder name

// Exported function (accessible from other packages)
func SampleFunction2() {
    // Function logic
}

// Unexported function (only accessible within the same package)
func sampleFunction1() {
    // Function logic
}
Enter fullscreen mode Exit fullscreen mode

Accessing Another Package from FolderA (Using FolderB's Function in FolderA)

Now, let's say we need to call FunctionB from FolderB inside FolderA/a.go:

Correct way to import and use FunctionB in FolderA/a.go

package FolderA

import "MainFolder/FolderB"

func sampleFunction1() {
    FolderB.FunctionB() // ✅ Correct usage
}

Enter fullscreen mode Exit fullscreen mode

*Accessing Global Variables from Another Package
*

If FolderC contains a global variable like DbPass, we can access it from FolderA as follows:

Example: Using Global Variables from FolderC in FolderA/a.go

package FolderA

import (
    "MainFolder/FolderB"
    "MainFolder/FolderC"
)

func sampleFunction1() {
    FolderB.FunctionB() // ✅ Function call

    // Accessing a global variable from FolderC
    if FolderC.DbPass == "some value" { // ✅ Variable must start with an uppercase letter
        // Do something
    }
}
Enter fullscreen mode Exit fullscreen mode

Key Takeaways

  • Package Naming Conventions: The package name should match the folder name (e.g., package FolderA inside FolderA/).
  • Exported Functions and Variables: Functions and variables that need to be accessed from other packages must start with an uppercase letter.
  • Lowercase Functions and Variables: Functions and variables that start with lowercase letters are private to the package.
  • Importing a Package: Use import "MainFolder/FolderX" to access other packages in the project.
  • Calling a Function from Another Package: Use PackageName.FunctionName() syntax.

Top comments (0)