官方说明,地址https://golang.org/doc/install
SDK下载
在官网 http://golang.org(可能需要翻墙) 直接下载对应操作系统的安装包安装即可。此处以Mac为例,下载pkg格式的最新安装包,直接运行,按提示完成安装。
安装过程,非常简单
安装完成后,可以打开终端,输入go,检测Golang SDK是否安装成功。
输入 go version
看到显示下图,即表示成功。
环境变量配置
根据官网说明,需要配置环境变量,原文内容见截图:
从文中内容可见(本人英文不是太好,如有错误,见笑了),默认安装后,路径为/usr/local/go。此时仅需要在环境变量PATH后追加/usr/local/go/bin即可。
当自定义了安装路径时,先添加一个GOROOT变量,指向自定义的安装路径后,再在PATH后追加$GOROOT/bin,如果是windows系统,是%GOROOT%。
工作空间workspace
官方原文:https://golang.org/doc/code.html
A workspace is a directory hierarchy with three directories at its root:
src contains Go source files,
pkg contains package objects, and
bin contains executable commands.
由此可见,workspace应当包含三个目录,分别是src(包含源文件)、pkg(相关包)、bin(执行文件)。
The go tool builds source packages and installs the resulting binaries to the pkg and bin directories.
The src subdirectory typically contains multiple version control repositories (such as for Git or Mercurial) that track the development of one or more source packages.
To give you an idea of how a workspace looks in practice, here's an example:
The tree above shows a workspace containing two repositories (example and image). The example repository contains two commands (hello and outyet) and one library (stringutil). The image repository contains the bmp package and several others.
A typical workspace contains many source repositories containing many packages and commands. Most Go programmers keep all their Go source code and dependencies in a single workspace.
Commands and libraries are built from different kinds of source packages. We will discuss the distinction later.
环境变量GOPATH
The GOPATH environment variable
The GOPATH environment variable specifies the location of your workspace. It defaults to a directory named goinside your home directory, so $HOME/go on Unix, $home/go on Plan 9, and %USERPROFILE%\go (usually C:\Users\YourName\go) on Windows.
If you would like to work in a different location, you will need to set GOPATH to the path to that directory. (Another common setup is to set GOPATH=$HOME.) Note that GOPATH must not be the same path as your Go installation.
The command go env GOPATH prints the effective current GOPATH; it prints the default location if the environment variable is unset.
通过命令go env GOPATH可以查看当前GOPATH变量指向的路径。
For convenience, add the workspace's bin subdirectory to your PATH:
为了方便起见,将工作空间的bin子目录添加到路径中
The scripts in the rest of this document use $GOPATH instead of $(go env GOPATH) for brevity. To make the scripts run as written if you have not set GOPATH, you can substitute $HOME/go in those commands or else run:
本文其余部分中的脚本使用$GOPATH而不是$(go env GOPATH)来简化。如果没有设置GOPATH,要使脚本按照编写的方式运行,可以在这些命令中替换$HOME/go,否则就运行
To learn more about the GOPATH environment variable, see 'go help go path'.
想了解更多关于GOPATH环境变量,查看'go help go path'
To use a custom workspace location, set the GOPATH environment variable.
使用自定义工作空间位置,需要设计GOPATH环境变量。
GOPATH是用来告诉Golang命令和其他工具,在哪里可以找到你系统上的Go包目录。
GOPATH是一个路径列表,类似于PATH的设置
GOPATH=/home/User/go:/home/User/workspace_go
每个列表(路径)其实就是一个workspace。
官方是这样介绍GOPATH的:
https://golang.org/wiki/SettingGOPATH
翻译:
GOPATH环境变量指定工作空间的位置。如果没有设置GOPATH,则假定它在Unix系统上为$HOME/go,在Windows上为%USERPROFILE%\go。如果希望使用自定义位置作为工作区,可以设置GOPATH环境变量。这个页面解释了如何在各种平台上设置这个变量。
注意:GOPATH的路径不能与GO的安装路径相同!
Unix systems
GOPATH can be any directory on your system. In Unix examples, we will set it to $HOME/go (the default since Go 1.8). Note that GOPATH must not be the same path as your Go installation. Another common setup is to set GOPATH=$HOME.
Bash
Edit your ~/.bash_profile to add the following line:
Save and exit your editor. Then, source your ~/.bash_profile.
Zsh
Edit your ~/.zshrc file to add the following line:
Save and exit your editor. Then, source your ~/.zshrc.
fish
The -x is used to specify that this variable should be exported and the -U makes this a universal variable, available to all sessions and persistent.
Windows
Your workspace can be located wherever you like, but we'll use C:\go-work in this example.
NOTE: GOPATH must not be the same path as your Go installation.
Create folder at C:\go-work.
Right click on "Start" and click on "Control Panel". Select "System and Security", then click on "System".
From the menu on the left, select the "Advanced systems settings".
Click the "Environment Variables" button at the bottom.
Click "New" from the "User variables" section.
Type GOPATH into the "Variable name" field.
Type C:\go-work into the "Variable value" field.
Click OK.
Windows 10
There is a faster way to edit Environment Variables via search:
Left click on "Search" and type env or environment.
Select "Edit environment variables for your account".
... and follow steps above.
网友评论