Golang import local package: modules, paths, and another directory

Golang import local package and go import local package with Go modules: golang import package from another directory under the module root, golang use local package and golang local package import paths, golang import local file vs packages, go get local package and replace, go local import, and golang how to import local package with exports.

Published

Updated

Read time 3 min read

Reviewed byDeepak Prasad

Golang import local package: modules, paths, and another directory

If you search golang import local package, go import local package, golang how to import local package, import local package golang, or go local import, you are usually wiring a module path in an import block to a directory under your project. Golang import package from another directory and golang local package are the same idea: the import string is your/module/path plus /subdir for each package folder. Golang import local file is a common misconception—Go does not import single files across packages. Golang use local package and go get local package come up when you add a sibling library or a vendored path; modules handle that with replace or a single repo layout. For packaging concepts, see golang packaging; for GOPATH-era layout, see GOPATH vs GOROOT (today’s default workflow is modules, not src/workspace/...).

Tested with Go 1.24 on Linux.


Create a module and directories:

bash
mkdir -p myapp/lib myapp/lib/extra
cd myapp
go mod init example.com/myapp

Typical tree:

text
myapp/
  go.mod                 # module example.com/myapp
  main.go                # package main
  lib/
    hello.go             # package lib
  lib/extra/
    more.go              # package extra

go.mod starts with the module line (a go toolchain line is added by go mod init for your installed version):

text
module example.com/myapp

golang import local package: the import path

Every import string starts with the module path from go.mod, then continues with the subdirectories that hold the package (without repeating the module folder name on disk). From main.go:

go
package main

import (
	"example.com/myapp/lib"
	"example.com/myapp/lib/extra"
)

func main() {
	lib.Hello()
	extra.More()
}

lib/hello.go:

go
package lib

import "fmt"

func Hello() {
	fmt.Println("from lib")
}

lib/extra/more.go:

go
package extra

import "fmt"

func More() {
	fmt.Println("from lib/extra")
}

Run go run . from myapp/; output lists both lines. That is the idiomatic answer to golang import package from another directory: the directory lib/extra maps to import path example.com/myapp/lib/extra.


Exports: why golang use local package can fail

Only capitalized names are visible outside the package. If lib defined func hello() (lowercase), main could not call lib.hello()—the compiler reports undefined. This matches the language spec for exported identifiers.


go get local package and replace (outside the tree)

If a second module lives next to your app, point to it from go.mod:

text
replace example.com/other => ../other

Then require example.com/other v0.0.0 and go mod tidy lets import "example.com/other/pkg" resolve to ../other/pkg on disk. That is the usual answer when people say go get local package for a path that is not published to a proxy.


Summary

Golang import local package in 2026 means: run go mod init, put each package in its own directory, and import module/path/subdir. Go import local package and golang local package searches are satisfied by that mapping—there is no parallel GOPATH src/workspace layout required. Golang import local file is not a separate mechanism: you always import a package directory. Golang import package from another directory is just a longer import path under the same module. go get local package matters when another module is on disk; use a replace directive to teach the toolchain where it lives, and remember exported names so golang use local package compiles.


References


Frequently Asked Questions

1. How do I golang import local package or go import local package today?

Use a Go module (go mod init module/path), put each package in its own directory with a matching package clause, then import with import "module/path/subdir"—the string is the module path plus the subdirectory holding that package.

2. Can I golang import local file without a folder?

No: Go imports packages (directories), not individual .go files. Put the file in a directory and use the directory’s package name and module import path.

3. What does golang import package from another directory mean in modules?

Any subdirectory of the module tree that contains .go files with the same package name becomes an import path module/path/to/dir if the files belong to one package, or split into multiple packages in nested dirs as needed.

4. How is go get local package used with a local library?

Inside the same module you normally do not go get yourself; use import. For a separate folder outside the module, add a replace directive in go.mod pointing at a relative or absolute path, then go get / go mod tidy resolves it.

5. Why does golang use local package fail with undefined?

Names starting with a lowercase letter are package-private; callers in another package must use exported identifiers (capital first letter) or stay in the same package.
Tuan Nguyen

Data Scientist

Proficient in Golang, Python, Java, MongoDB, Selenium, Spring Boot, Kubernetes, Scrapy, API development, Docker, Data Scraping, PrimeFaces, Linux, Data Structures, and Data Mining. With expertise …