Go
Beginner
1 min read
Setting Up a Go Module
Example
// Step 1: initialise a new module (run in terminal)
// $ go mod init github.com/example/myapp
// go.mod (auto-generated)
// module github.com/example/myapp
// go 1.22
// Step 2: project layout
// myapp/
// go.mod
// go.sum
// main.go
// internal/
// greet/
// greet.go
// internal/greet/greet.go
package greet
import "fmt"
// Hello returns a greeting string.
func Hello(name string) string {
return fmt.Sprintf("Hello, %s!", name)
}
// main.go
package main
import (
"fmt"
"github.com/example/myapp/internal/greet"
)
func main() {
msg := greet.Hello("world")
fmt.Println(msg)
}
// Step 3: run
// $ go run .
// Hello, world!