Packages and Modules
Go Modules
Go Modules are the official dependency management system introduced in Go 1.11 and became the default in Go 1.16+. They provide a way to manage versioned dependencies and project structure.
Key Concepts
-
A module is a collection of Go packages stored in a file tree with a
go.mod
file at its root. -
Each module defines:
- Its own module path (usually a repository URL like
github.com/user/project
) - Its dependencies (along with their versions)
- Its own module path (usually a repository URL like
Important Files
go.mod
: Defines the module and its dependenciesgo.sum
: Records checksums of modules to verify integrity
Commands
go mod init <module-path>
: Initializes a new modulego mod tidy
: Adds missing and removes unused dependenciesgo get <package>@version
: Adds or upgrades a dependencygo build
,go run
, etc., will automatically download necessary modules
Example
go mod init github.com/abhishek/myapp
This creates:
module github.com/abhishek/myapp
go 1.21
package main
In Go, every file must declare a package
. The main
package is special. It tells the Go compiler that this package is executable, not a shared library.
Rules
- Only the
main
package can produce an executable binary. - The
main
package must contain a function calledmain()
.
Usage
Typical for programs meant to be run (e.g., CLI apps, web servers, etc.)
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
If we change package main
to something else (like package utils
), Go will not generate an executable for it. It will be a reusable library.
func main()
Purpose
The main()
function is the entry point of a Go application.
Rules
- It must have the signature
func main()
- It cannot take arguments or return values
- We can define other functions and call them from
main()
- There can only be one
main()
function permain
package
Behavior
- When we run
go run .
or build and execute the binary, Go looks formain.main()
to start execution.
Example
package main
import "fmt"
func greet(name string) {
fmt.Printf("Hello, %s!\n", name)
}
func main() {
greet("Abhishek")
}