go to import third-party packages

The best way to learn is to practice. Let's depth study by importing gin package.

surroundings

  • go 1.13.5

  • goland 2019.3.1

  • Manjaro-gnome3.34.2

Environment Variables

First Recall that when we install goland, set up a few of the more important environmental variables, namely GOROOT and GOPATH.

GOROOT easier to understand. That is the installation directory we go language standard library what's on the inside of it. In linux, the default install to / usr / lib / go, select the SDK when creating a project we have is this.

GOPATH can be understood as the working directory. Under GOPATH specified directory generally have three subdirectories. as follows:

GOPATH    //go工作目录
  |
  |--bin     //编译后生成的可执行文件
  |
  |--pkg     //编译后生成的包/库文件
  |
  |--src     //项目的源代码文件

bin directory is easy to understand, and pkg package is the place to put third-party libraries, like bag after go get on the inside. src directory of the project is to put the source code, usually the more items are put under the src directory and unified management of these projects, which is a common bin and pkg, if you put the project in a different directory, then these projects are put to GOPATH, it is clear there will be conflict. It is generally not recommended.

We can enter in a terminal go envviewing environment variable, the following is my configuration directory:

Practice guide package

Now let's create a new project, the actual try.

Then create a main file, enter:

package main

import "github.com/gin-gonic/gin"

func main() {
    r := gin.Default()
    r.GET("/ping", func(c *gin.Context) {
        c.JSON(200, gin.H{
            "message": "pong",
        })
    })
    r.Run() 
}

Then open a terminal and type go mod init, run directly, you will find a direct package download import of, and this time if the direct run, then, is found to be directly run, but found newspaper red, the input will not auto-complete.

This time simply open setting, to go mod tick, as follows. Then run it directly, or reopen ide will not be reported because of the red.

Of course, we can also manually download their own, just use go get command.

go get -u github.com/gin-gonic/gin

But found no response, input go envview found a variable called GO111MODULE, this stuff is doing it, it is used to configure the go mod with, such as when to go path, goroot go, or looking directly go mod in. There are three states.

  • GO111MODULE=autoThis is the default, automatically determine whether to open the go mod based on some cases.
  • GO111MODULE=onOpen the go mod
  • GO111MODULE=offClose go mod

于是我们输入go env -w GO111MODULE=on打开go mod,再次输入go get -u github.com/gin-gonic/gin,但是会发现下载的速度简直感人,这是因为服务器在国外的嘛,你懂得,我们可以设置代理。

go env -w GOPROXY=https://goproxy.cn,direct

再次go get,发现速度飞起,尝试run一下,如果出现报红问题,像上面那样解决一下。

当然我们也可以项目放到GOPATH目录以外。如下

同样的,如果要导包的话,像上面那样使用go mod就行了。

我们可以进入GOPATH目录下的pkg目录,会发现我们导入的包在这里面的。

我们也可以进入goland,

发现外部库有两个,一个是SDK,也就是go的安装目录(GOROOT),标准库就在这里的。另一个是go mod,就是我们刚刚导入的,放到GOPATH/pkg下的包。

Guess you like

Origin www.cnblogs.com/just-save/p/12172947.html