Avatar ·

How to properly import local packages in Go (Golang) — step-by-step guide

Hello everyone! I recently started learning Go and ran into a problem that completely baffles me. I'm trying to organize code in multiple files within one project, but importing local packages doesn't work. Here's my project structure: ``` myproject/ ├── main.go └── helpers/ └── math.go ``` In the file `helpers/math.go`, I declared the package `package helpers` and the function `Add(a, b int) int`. In `main.go`, I'm trying to import this package: ```go package main import ( "fmt" "myproject/helpers" // tried different paths ) func main() { result := helpers.Add(2, 3) fmt.Println(result) } ``` But when I run `go run main.go`, I get an error: ``` main.go:4:2: package myproject/helpers is not in GOROOT (/usr/local/go/src/myproject/helpers) ``` I've tried different path variants: `./helpers`, `../helpers`, `helpers` — nothing helps. I've heard something about modules, but I don't understand how to set them up correctly. Please advise how to properly