Use Go Module Step By Step Guide
It is frustrating using Go Module for new Goer. This guide will not talk about too much how does Go find the package/modules but will show you step-by-step how to create and test a module in a Windows system.
Assumption
Go version >= 1.16
Go is set up on your laptop
I use folder
F:\dev\golang\go_basic\moduledemo
for testinggitbash as terminal
Step-by-Step Guide
Enable module mode
# Check if go module mode
$ go env | grep GO111MODULE
set GO111MODULE=auto
# or
go env GO111MODULE
# If the value is off, plz use the gitbach command to set it to auto or on
export GO111MODULE=auto
export GO111MODULE=on
# For cmd.exe, kindly use set
set GO111MODULE=on
echo %GO111MODULE%
# Or use go command
go env -w GO111MODULE=on
go env GO111MODULE=on
Create a project folder, init module
# Create a new project folder at any place
mkdir moduledemo
cd moduledemo
# init the module with name moduledemo at the folder
go mod init moduledemo
Create the test source
$ tree
.
|-- boo
| `-- Boo.go => this is module, called by main.go
|-- go.mod => this file is created via go mod init
`-- main.go
Source Code
main.go
package main
import (
"fmt"
"moduledemo/boo"
)
func main() {
boo.Boo()
fmt.Println("this msg is from main")
}
Boo.go
package boo
import "fmt"
func Boo() {
fmt.Println("this msg is from boo module")
}
go.mod
module moduledemo
go 1.20