Goland | Use Goland IDE go mod to build projects

No matter what programming language you learn, the four steps you will experience at the beginning

  • Development tool installation
  • IDE installation
  • Set dependency/package domestic mirror
  • Project build tool, manage dependencies/packages

1. Golang development tool installation

https://golang.org/ official website (requires their own means)
https://github.com/golang/go github address
http://docscn.studygolang.com/ golang中文网

Two, Goland IDE installation

https://www.jetbrains.com/zh-cn/go/

Three, set dependency/package domestic mirror

Insert picture description here
Common mirror address:

阿里云: https://mirrors.aliyun.com/goproxy
 
微软: https://goproxy.io
 
七牛云: https://goproxy.cn
 
GoCenter: https://gocenter.io

Modify GOPATHpath

windowsOn the default path %USERPROFILE%\go
mac&linuxOn the default path$HOME/go

What I demonstrate here is to set the GOPATHpath toD:\gopath

D:\gopathUsed to store go moddownloaded third-party dependency packages
D:\GolandProjectsUsed to store projects

// 始终开启go modules
go env -w GO111MODULE=on
// 设置阿里云镜像
go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/
// 设置gopath
go env -w GOPATH=D:\gopath

Will be GOPATHadded to environment variables

Insert picture description here

Gopath settings in Goland

Insert picture description here

Four, project construction tools, manage dependencies/packages

Use go modmethod to build project advantages

  1. Automatically download dependencies
  2. The project does not have to be placed in GOPATH/src
  3. A go.mod file will be generated in the project, listing package dependencies
  4. All third-party packages will accurately specify the version number
  5. For packages that have been transferred, you can use the replace statement to replace, no need to change the code

Reference article

Relations GOROOT, GOPATH, Go-Modules- three introduced
https://blog.csdn.net/y1534414425/article/details/108767310

1, in the go.modedit

Insert picture description here
Go.mod can write the following keywords:

  • module
    defines the module path
  • go
    definition go language version
  • require
    specifies the dependent package, the default is the latest version, you can specify the version number
  • exclude
    exclude the package and its version
  • replace
    use a different package version and replace the original package version
  • Annotation
    // Single-line annotation
    /* Multi-line annotation*/
    indirect represents dependent packages that are indirectly imported

2, the command-line go getaddress

go get
As long as there are open go modulesfeature, go getit will not be the same as in the previous GOPATH/srccase files placed kit, but will be placed GOPATH/pkg/modthere, and go.modwill write the introduction, so do not use it go mod downloadcommanded.

Guess you like

Origin blog.csdn.net/y1534414425/article/details/108774594