Why use go mod
go mod is an official package management tool newly introduced in version 1.11 of Golang. It is used to solve the problem that there is no place to record the specific version of the dependent package before, so as to facilitate the management of dependent packages.
how to use go mod
Create a new go file as follows
package main
import (
iris "github.com/kataras/iris/v12"
)
func main() {
app := iris.New()
app.Get("/", func(ctx iris.Context) {
ctx.HTML("<h1> Hello World </h1>")
})
app.Run(iris.Addr(":8080"))
}
1. Open the CMD command prompt box and set temporary environment variables
set GO111MODULE=on
set GOPROXY=https://mirrors.aliyun.com/goproxy
# 注意: 也可以使用 https://goproxy.io
2. Executing go get github.com/kataras/iris can also add -v -u, as follows
At this time, all dependencies are downloaded.
It is worth noting that these packages are not downloaded into the GOPATH/src directory, but in the GOPATH/pkg/mod directory.
3. Use initialization to generate go.mod file
go mod init
4. Add dependencies and run the project
Execute go run test.go
and run the code and you will find that go mod will automatically find the dependencies and download them automatically
go run test.go
5. Verify project operation
Visit http://localhost:8080/
References: https://blog.csdn.net/qq_38151401/article/details/105780251