Modules with go mod

Contents
go mod init
go mod tidy
Run inside a module
Import your own package

go mod init

A module is a project with a go.mod file that records the module path and the Go version.

Create a project folder, open it in the terminal, and initialize the module with a path that mirrors the future repository location.

go mod init example/hello

go: creating new go.mod: module example/hello

module example/hello go 1.21

go mod tidy

The go mod tidy command adds missing requirements and removes unused ones, so go.mod always matches the actual imports.

Run it after adding or deleting dependencies, and commit the resulting go.mod and go.sum files with the project.

go mod tidy

A clean project prints nothing, which means there is nothing to fix.

Run inside a module

Inside a module, commands accept a package pattern instead of file names, and a single dot means the current folder.

package main import "fmt" func main() { fmt.Println("Hello from a module") }

go run .

Hello from a module

Import your own package

Larger programs split code into packages, where each folder holds one package and the import path extends the module path with the folder name.

Save the helper below as greet/greet.go and import it from main.go by the full path.

package greet func Hello() string { return "Hello from greet" }

package main import ( "example/hello/greet" "fmt" ) func main() { fmt.Println(greet.Hello()) }

go run .

Hello from greet

Next: Variables

Article author: Arthur Isaev

Related articles
Golang
Go course: zero to hero in 50 lessons
What is Go and where it runs
Install Go and check the version
Your first Go program
Modules with go mod
Variables
Constants
Types
Numbers
Strings
Conditions with if
Switch in Go
Loops with for
Arrays in Go
Slices in Go
Maps in Go
Functions in Go
Errors as values
Pointers in Go
Structs in Go
Methods in Go
Interfaces in Go
Embedding instead of inheritance
Generics in Go
Packages and imports
The go toolchain
Testing with go test
Advanced errors
defer in Go
Strings, bytes and runes
Time in Go
JSON
Files and IO
HTTP servers
HTTP clients
Context
Goroutines
Channels
select and sync
CLI: args, flags, env
Logging
Regular expressions
Sorting
Concurrency patterns
Databases with database/sql
Benchmarks and profiling
Toolchain and CI
Project layout
Capstone: wordfreq CLI
Capstone: JSON API server
Hero roadmap: the whole course on one page
Install Go on Windows 11
Install Go on Ubuntu
Install Go on Rocky Linux
Install Go on macOS
Install Go on FreeBSD
Go in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: