Go Tools — go get
Continuing from the previous article about Go Tools — env, fmt, imports,
Today, I would like to write about “go get” which is the most frequently used one for dependency management.
the command is executed in the form of
$ go get <package@version>
This command lets you install the desired dependency with the specific version. Otherwise, you can simply add a suffix like @latest
to fetch the latest version, or @none
to uninstall the dependency.
For example, let’s say you have a clean directory with zero dependencies in $GOPATH/pkg/mod directory.
in unix system, that’d be $HOME/go/pkg/mod
→ /Users/yeongseokkim/go/pkg/mod
Confirming that there are no files or directories before doing download test
(to clean up, you can do it by $ go clean -modcache )
➜ mod tree .
.
0 directories, 0 files
and then execute the following command to download a module with a specific version in a Go application project .
➜ go-app-one git:(main) go get github.com/youngstone89/go-module-one@v0.1.1
go: downloading github.com/youngstone89/go-module-one v0.1.1
Once the download is completed, you can see changes in the $GOPATH/pkg/mod directory like this
➜ mod tree .
.
├── cache
│ └── download
│ └── github.com
│ └── youngstone89
│ └── go-module-one
│ └── @v
│ ├── list
│ ├── v0.1.1.info
│ ├── v0.1.1.lock
│ ├── v0.1.1.mod
│ ├── v0.1.1.zip
│ └── v0.1.1.ziphash
└── github.com
└── youngstone89
└── go-module-one@v0.1.1
├── README.md
├── go.mod
└── pkg
└── kafka
└── consumer.go
cache
directory and github.com
got newly created to store the downloaded dependency.
Now you are ready to import the downloaded dependency and implement.
This is how go get
works.
Thanks!