Go mod is a new feature added to Golang1.11. It can be used to separate projects from Gopath, so projects don’t have to be placed in the SRC directory of Gopath. Go mod has the following commands:

1. Switch to domestic sources

For well-known reasons, pulling github open source code using Go Get often fails. There are two ways to set up the domestic proxy source:

  • Set in IDE (Goland) :

  • Setting environment variables on Windows (Linux is similar)

Create GOPROXY in the system variable and set it to:goproxy.io

2. Go mod pull gitLab private warehouse Settings

  • Create personal_access_tokens on GitLab. Create Personal_access_tokens in GitLab using Settings->Access Tokens. It is recommended to select read_Repositoty in Scopes

  • Git config –global HTTP. Extraheader “private-token: [your PRIVATE TOKEN]”
  • If your gitlab does not support HTTPS, you need to run the following command:

Gitlab clone with SSH address (insteadOf the insteadOf HTTP address). Git.

  • It can then be executed from the command line:

At this point your PKG /mod in Gopath will pull code from GitLab.

Git config can also be directly opened.

3. Go get it

  • Download project dependencies

      go get ./...
    Copy the code
  • Pull the latest version (tag is preferred)

     go get golang.org/x/text@latest
    Copy the code
  • Pull the latest commit from the master branch

     go get golang.org/x/test@master
    Copy the code
  • Pull commit with tag V0.3.2

    Go get golang.org/x/[email protected]Copy the code
  • update

     go get -u
    Copy the code

4. Go mod Basic operations

  • Initialize a mod with the module name of your project

    Go mod init Module nameCopy the code
  • Download the mod to the local cache

    Currently, all module version data is cached in @gopath/PKG /mod and @gopath/PKG /sum go Mod DownloadCopy the code

5. Go mod is used in IDE

Although we configured the GitLab private repository above, we can pull the Code of GitLab by go Get, but sometimes it is inevitable that the address of gitLab private repository has been configured in the mod file, and it can still be pulled automatically (for example, my GitLab does not support HTTPS requests, It can only be done over SSH or HTTP, so even if require writes the gitlab code address in the mod file, the go mod will fail to automatically pull because it is requested via HTTPS by default. In this case, you can use git clone to pull down the gitlab code and put it in the @gopath /mod folder. Then in the mod file replace the gitlab repository address with the local address, as shown below:

  • Add to require:

  • In replace add: