golang Quick Start [7.1] - Project and dependency management -gopath

preamble

Foreword

  • In the previous article, we introduced the configuration go language development environment

  • In this chapter, we will introduce the project structure go language, project management and dependency management. In this article, we focusgopath

What is gopath

  • In the go-language development environment configuration article, we introduced the configuration gopathand gorootthe steps environment variables, but not its in-depth explanation. You may be in a terminal go envor go env gopathto view a particular configuration

C:\Windows\system32> go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\jackson\AppData\Local\go-build
set GOENV=C:\Users\jackson\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\jackson\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=c:\go
...
  • After go1.8, if you do not specify gopath, the gopath is the default.

    • In mac, linux is$HOME/go

    • windows next to%USERPROFILE%\g

  • gopathIt can be understood as the working language of space go inside the store src, bin, pkgthree folders

go/
├── bin
├── pkg
└── src
  • $GOPATH/binDirectory stores by go installbinaries installed. The operating system usesPATH variable

  • $GOPATH/pkgDirectory stores precompiled obj file (file names based on different operating systems and different, such as under mac darwin_amd64) to speed up the follow-up to the compiler. Most developers do not need to access this directory. Will be introduced later, mod files will be stored under pkg go moduledependence.

  • $GOPATH/srcDirectory to store our project go code. Usually contain many version control repository (for example, managed by Git), each repository contains one or more of the package, each package contains one or more of the Go source files in a directory.

  • Thus, the entire path looks like:

go/
├── bin
     └── main.exe
├── pkg
     ├── darwin_amd64
     └── mod
└── src
    ├── github.com
    │   ├── tylfin
    │   │   ├── dynatomic
    │   │   └── geospy
    │   └── uudashr
    │       └── gopkgs
    └── golang.org
        └── x
            └── tools
  • gopath have more effect, when we want to go get the code from github project or elsewhere, we can use the go getcommand. At this point the program will default to the code stored in the $GOPATH/srcdirectory. Pulling example go get github.com/dreamerjackson/theWayToGolang, the directory structure is as follows:

go/
├── bin
├── pkg
└── src
    └── github.com
           └── dreamerjackson
                     └── theWayToGolang
  • When we use go getthe -uparameter, and all other items will be the project depends to a batch download $GOPATH/srcdirectory

  • Another feature gopath is clear package import position. We previously introduced, go through the organization code package, in helloworl program, we import go language built-in fmtpackage. When we want to import third party should be how to do it? In fact, if we import the project as a third-party packages, such as

import "blue/red"

The actual quote is the $GOPATH/src/blue/redcode from a file.

Similarly, if imported as

import "github.com/gobuffalo/buffalo"

The actual quote is the $GOPATH/src/github.com/gobuffalo/buffalocode from a file.

Here we use an example to illustrate the package of the third party

  • First, $GOPATH/srccreate a new folder mymath, create a new file in the folderadd.go

» mkdir mymath
» cd mymath
» touch add.go

add.go reads as follows, should pay attention to the exported functions must be capitalized, which is the language of the rules go.

package mymath

func Add(a int, b int) int {
    return a + b
}

Then in $GOPATH/srccreating a main.go file, which can call a complete mymathpackage of addfunctions.

package main

import (
    "fmt"
    "mymath"
)

func main() {
    result := mymath.Add(1, 2)
    fmt.Println("result:", result)
}
  • By go run main.goto run output 1+2results3

gopath merits

  • Compared to other tedious configuration language, go language workspace gopathconfiguration is relatively simple, easy to understand

  • gopath so that the file system to organize the entire code is more concise, structured, but limited to a single workspace.

  • gopath并没有解决版本依赖的问题,而将其留给了其他工具去实现。正因为如此,gopath中的代码就是一个唯一的master分支,并且强制使用各个模块最新的代码。

总结

  • 本文介绍了gopath的含义、功能、优劣、以及如何通过GOPATH来组织项目,导入第三方库。

  • 在go1.13之后,go官方已经开始全面拥抱go module.我们在下文中,将介绍go module的原理和用法,以及如何通过go module进行go语言的依赖管理与项目组织。

参考资料


Guess you like

Origin blog.51cto.com/13784902/2475102