当前位置:   article > 正文

gopath_了解GOPATH

$gopath

gopath

介绍 (Introduction)

This article will walk you through understanding what the GOPATH is, how it works, and how to set it up. This is a crucial step for setting up a Go development environment, as well as understanding how Go finds, installs, and builds source files. In this article we will use GOPATH when referring to the concept of the folder structure we will be discussing. We will use $GOPATH to refer to the environment variable that Go uses to find the folder structure.

本文将GOPATH您了解什么是GOPATH ,它如何工作以及如何进行设置。 这是设置Go开发环境以及了解Go如何查找,安装和构建源文件的关键步骤。 在本文中,当我们将要讨论的文件夹结构的概念时,我们将使用GOPATH 。 我们将使用$GOPATH引用Go用来查找文件夹结构的环境变量。

A Go Workspace is how Go manages our source files, compiled binaries, and cached objects used for faster compilation later. It is typical, and also advised, to have only one Go Workspace, though it is possible to have multiple spaces. The GOPATH acts as the root folder of a workspace.

Go工作区是Go管理我们的源文件,已编译二进制文件和用于以后更快编译的缓存对象的方式。 尽管可能有多个空间,但通常也建议只有一个Go工作空间。 GOPATH充当工作空间的根文件夹。

设置$GOPATH环境变量 (Setting the $GOPATH Environment Variable)

The $GOPATH environment variable lists places for Go to look for Go Workspaces.

$GOPATH环境变量列出了Go查找Go Workspaces的位置。

By default, Go assumes our GOPATH location is at $HOME/go, where $HOME is the root directory of our user account on our computer. We can change this by setting the $GOPATH environment variable. For further study, follow this tutorial on reading and setting environment variables in Linux.

默认情况下,Go假定我们的GOPATH位置为$HOME/go ,其中$HOME是计算机上用户帐户的根目录。 我们可以通过设置$GOPATH环境变量来更改它。 要进一步研究,请遵循本教程,以了解如何在Linux中阅读和设置环境变量

For more information on setting the $GOPATH variable, see the Go documentation.

有关设置$GOPATH变量的更多信息,请参见Go 文档

Furthermore, this series walks through installing Go and setting up a Go development environment.

此外,本系列文章还将介绍如何安装Go并设置Go开发环境。

$GOPATH不是$GOROOT ($GOPATH Is Not $GOROOT)

The $GOROOT is where Go’s code, compiler, and tooling lives — this is not our source code. The $GOROOT is usually something like /usr/local/go. Our $GOPATH is usually something like $HOME/go.

$GOROOT是Go的代码,编译器和工具的存放地-这不是我们的源代码。 $GOROOT通常类似于/usr/local/go 。 我们的$GOPATH通常类似于$HOME/go

While we don’t need to specifically set up the $GOROOT variable anymore, it is still referenced in older materials.

尽管我们不再需要专门设置$GOROOT变量,但是在较旧的资料中仍然引用了它。

Now, let’s discuss the structure of the Go Workspace.

现在,让我们讨论Go Workspace的结构。

Go工作区剖析 (Anatomy of the Go Workspace)

Inside of a Go Workspace, or GOPATH, there are three directories: bin, pkg, and src. Each of these directories has special meaning to the Go tool chain.

在Go Workspace或GOPATH ,有三个目录: binpkgsrc 。 这些目录中的每一个对于Go工具链都有特殊的含义。

  1. .
  2. ├── bin
  3. ├── pkg
  4. └── src
  5. └── github.com/foo/bar
  6. └── bar.go

Let’s take a look at each of these directories.

让我们看一下每个目录。

The $GOPATH/bin directory is where Go places binaries that go install compiles. Our operating system uses the $PATH environment variable to find binary applications that can execute without a full path. It is recommended to add this directory to our global $PATH variable.

$GOPATH/bin目录是Go放置go install编译二进制文件的位置。 我们的操作系统使用$PATH环境变量查找无需完整路径即可执行的二进制应用程序。 建议将此目录添加到我们的全局$PATH变量中。

For example, if we don’t add $GOPATH/bin to $PATH to execute a program from there, we would need to run:

例如,如果我们不将$GOPATH/bin添加到$PATH以从那里执行程序,则需要运行:

  • $GOPATH/bin/myapp

    $ GOPATH / bin / myapp

When $GOPATH/bin is added to $PATH we can make the same call like such:

$GOPATH/bin添加到$PATH我们可以像这样进行相同的调用:

  • myapp

    myapp

The $GOPATH/pkg directory is where Go stores pre-compiled object files to speed up subsequent compiling of programs. Typically, most developers won’t need to access this directory. If you experience issues with compilation, you can safely delete this directory and Go will then rebuild it.

$GOPATH/pkg目录是Go存放预编译目标文件的目录,以加快程序的后续编译速度。 通常,大多数开发人员不需要访问该目录。 如果您遇到编译问题,则可以安全地删除此目录,然后Go会重新构建它。

The src directory is where all of our .go files, or source code, must be located. This shouldn’t be confused with the source code the Go tooling uses, which is located at the $GOROOT. As we write Go applications, packages, and libraries, we will place these files under $GOPATH/src/path/to/code.

src目录是我们所有.go文件或源代码所在的位置。 这不应与Go工具使用的源代码(位于$GOROOT处) $GOROOT 。 在编写Go应用程序,程序包和库时,我们会将这些文件放在$GOPATH/src/path/to/code

什么是包裹? (What Are Packages?)

Go code is organized in packages. A package represents all the files in a single directory on disk. One directory can contain only certain files from the same package. Packages are stored, with all user-written Go source files, under the $GOPATH/src directory. We can understand package resolution by importing different packages.

Go代码以包的形式组织。 包代表磁盘上单个目录中的所有文件。 一个目录只能包含同一软件包中的某些文件。 软件包和所有用户编写的Go源文件一起存储在$GOPATH/src目录下。 我们可以通过导入不同的软件包来了解软件包的解析。

If our code lives at $GOPATH/src/blue/red then its package name should be red.

如果我们的代码位于$GOPATH/src/blue/red则其包名称应为red

The import statement for the red package would be:

red软件包的导入语句为:

import "blue/red"

Packages that live in source code repositories, like GitHub and BitBucket, have the full location of the repository as part of their import path.

驻留在诸如GitHub和BitBucket之类的源代码存储库中的软件包将存储库的完整位置作为其导入路径的一部分。

For example, we would import the source code at https://github.com/gobuffalo/buffalo using the following import path:

例如,我们将使用以下导入路径在https://github.com/gobuffalo/buffalo处导入源代码:

import "github.com/gobuffalo/buffalo"

Therefore, this source code would be in the following location on disk:

因此,此源代码将位于磁盘上的以下位置:

$GOPATH/src/github.com/gobuffalo/buffalo

结论 (Conclusion)

In this article we discussed the GOPATH as a set of folder’s that Go expects our source code to live within, as well as what those folders are, and what they contain. We discussed how to change that location from the default of $HOME/go to the user’s choice by setting the $GOPATH environment variable. Finally, we discussed how Go searches for packages within that folder structure.

在本文中,我们讨论了GOPATH ,它是Go希望其源代码GOPATH在其中的一组文件夹,以及这些文件夹及其所包含的内容。 我们讨论了如何通过设置$GOPATH环境变量将该位置从默认$HOME/go更改为用户的选择。 最后,我们讨论了Go如何在该文件夹结构内搜索软件包。

Introduced in Go 1.11, Go Modules aim to replace Go Workspaces and the GOPATH. While it is recommended to start using modules, some environments, such as corporate environments, may not be ready to use modules.

在Go 1.11中引入的Go模块旨在替代Go Workspaces和GOPATH 。 建议开始使用模块时,某些环境(例如公司环境)可能尚未准备好使用模块。

The GOPATH is one of the trickier aspects of setting up Go, but once it is set up, we can usually forget about it.

GOPATH是设置Go的棘手方面之一,但是一旦设置好,我们通常会忘记它。

翻译自: https://www.digitalocean.com/community/tutorials/understanding-the-gopath

gopath

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/974754
推荐阅读
相关标签
  

闽ICP备14008679号