1024programmer Java Best practices for Go language basic development environment

Best practices for Go language basic development environment

This article is one of the author’s Go language basic series. This article introduces what the author believes to be the best practices in the development environment;

This article is the author’s original work. Please indicate the source when reprinting;

GoPATH

Golang sets the relative path of the class library and source code through GoPATH. All source code referenced by Go must be placed Go to this path; Golang has agreed on two directories under the GoPATH path,

  • src

    Save project source code and third-party classes The source code of the library is usually

    ├──
    └── src
    └──
    ├─ ─ main.go
    └── …
    └──
    ├── main.go
    └── …

    Organize the project in this directory structure;

  • bin
    saves the built binary executable file; the directory structure is as follows,

    ├──
    └── bin
    project a main.exe

    └── src
    └──
    ├── project a main.go
    └── …
    └──
    ├── main.go
    └── …

Build two completely isolated projects

Pain points,

Golang sets a global path through the GoPath environment variable. According to Go convention, the source code and third-party packages of all projects are placed in the $GoPath/src directory. And jointly reference the same global class library; this is very unfavorable for the development of multiple projects, because usually independent projects need to be isolated from each other and need to reference their independent class libraries;

Scheme,

Goland provides Project GOPATH settings independent of the project, so that the GOPATH between different projects can be isolated;

First of all, we need to be clear that Golang requires , the path of the project source code must be $GoPath/src/; now, assuming that we have two independent projects a and b, then how can we create two independent projects through Goland? What about completely isolated projects?

  1. Create a project through Goland, assuming the project path is ~/workspace/golang/a;
  2. Set the Project Path of project a, as shown in the figure,

    Note that in the macos environment, after Golang is installed, the global Gopath will be set in the ~/mac/go path; here, we only need to set the project a itself through the Project Path GoPath is enough, the author’s path here is ~/workspace/golang/a; check,

    $ go env
    GOARCH =”amd64″
    GOBIN=””
    GOCACHE=”/Users/mac/Library/Caches/go-build”
    GOEXE=””
    GOFLAGS=””
    GOHOSTARCH=” amd64″
    GOHOSTOS=”darwin”
    GOOS=”darwin”
    GOPATH=”/Users/mac/workspace/golang/a:/Users/mac/go”

    Here we focus on GOPATH, which assigns both Project GOPATH and Global GOPATH paths to the environment variable GOPATH; it can be seen that Goland actually just resets GOPATH and changes the global and the project’s own GOPATH are combined into one and assigned to the environment variable GOPATH; note, the above check must be performed in Goland’s own Terminal, because the Project Path can only take effect in Goland’s own Terminal.

  3. Create the source code, hello.go, in project a. Note that according to Golang’s convention on source code, all source code (including the source code of third-party libraries) must be placed in $GoPath/src directory, so here, the path of the author’s source code is defined as $Project GoPath/src/a/hello.go, as shown in the figure, The only thing that makes the author feel awkward is that there are two a in the directory structure , the project name appears twice in the path, but there is nothing surprising. The first path a is the key path in Project GoPath, and the second path a represents the project name;
  4. To build the test, execute the build command go install through Goland’s Terminal,

    $ cd ~/workspace/golang/a/src/a/
    $ go install

    go install will build the go code under project a and generate the built binary executable file into the $Project GOPATH/bin/ directory,

    $ tree ~/workspace/golang/a
    /Users/mac/workspace/golang/a
    ├── bin
    │ └── a
    └── src
    └── a
    ├── hello.go
    └── modules

  5. Just build project b in the above way;

In this way, we achieve complete isolation between two different projects; Summarize, you can place the public class library in $Global GoPath/src, and then place the project-independent class library in $Project GoPath/src;

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/734657

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索