Welcome To Golang By Example

Importing package within the same module in Go (Golang)

Any package within the same module can be imported using the import path of module + directory containing that package. To illustrate lets create a module

go mod init learn

main.go

package main

import (
	"fmt"
	"learn/math"
)

func main() {
	fmt.Println(math.Add(1, 2))
}

math/math.go

package math

func Add(a, b int) int {
    return a + b
}

See how we have imported the math package in the main.go file

"learn/math"

Here the import path is import path of module which is learn +  directory containing the package which is math. Hence “learn/math” . Packages in nested directory can also be imported in the same way. The way it works is that since the prefix is the module import path, hence go will know that you are trying to import from the same module. So it will directly refer it instead of downloading it.