About Go Modules (vgo)

What is Go Modules?

Go's dependency management tool. Until a long time ago, dep was used a lot, but it was introduced from Go1.11 and seems to be used a lot.

By the way, it is a full-scale introduction from Go1.13, and Go1.11 to 1.12 are in the transition period, so if applicable, we will upgrade. You can also use it by checking the environment variables with the following command and setting ** GO111MODULE ** to ** on **.

$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
.
.
.  //Continue below

How to use Go Modules

We will follow a series of steps from the point of actually creating a project. First, create a new project in any directory in any directory.

Also, if you use Go Modules, you can put it outside GOPATH, but it seems that the save destination will be under GOPATH. → Reference article

(In my case: $ GOPATH / src / github.com/username/testproject)

mkdir $GOPATH/src/github.com/username/testproject

Go to the project you created and run go mod init.

go mod init github.com/username/testproject

I think a file called `` `go.mod``` will be generated in the same hierarchy as testproject.

go.mod


module github.com/username/testproject

go 1.**  //Each version

Here, the installation by go get such as packages will be described. I will try installing Gin **, which is a framework of ** Go.

go get github.com/gin-gonic/gin

There should be an addendum in the go.mod file. Import it in another file and use it as appropriate. A new file called `` go.sum``` will also be created. Dependencies are recorded here.

go.mod


module github.com/username/testproject

go 1.**  //Each version

require github.com/gin-gonic/gin v1.6.3

go.sum


github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14=
.
.
.  //Continue below

Remove package

Unused packages can be removed with the following command. Also, in such a case, it will tell you with an error.

go mod tidy github.com/gin-gonic/gin  //Example

Finally

This time, I left a minimum of Go Modules. Next, I will update the basic type of Go in several parts! I think I'll leave notes for other content from time to time! !!

Recommended Posts

About Go Modules (vgo)
About Go functions
About Go Interface
Learn about Go slices
Note about pointers (Go)
About Go control syntax
About Go GET request
[Golang] About Go language channel
About the basic type of Go
[Golang] About Go language Producer and Consumer