Golfing(Go语方)开发环境搭建与配置

栏目: 服务器 · 发布时间: 6年前

内容简介:官方说明,地址https://golang.org/doc/install在官网 http://golang.org(可能需要翻墙) 直接下载对应操作系统的安装包安装即可。此处以Mac为例,下载pkg格式的最新安装包,直接运行,按提示完成安装。安装过程,非常简单

官方说明,地址https://golang.org/doc/install

SDK下载

在官网 http://golang.org(可能需要翻墙) 直接下载对应操作系统的安装包安装即可。此处以Mac为例,下载pkg格式的最新安装包,直接运行,按提示完成安装。

Golfing(Go语方)开发环境搭建与配置
Golfing(Go语方)开发环境搭建与配置

安装过程,非常简单

Golfing(Go语方)开发环境搭建与配置
Golfing(Go语方)开发环境搭建与配置
Golfing(Go语方)开发环境搭建与配置
Golfing(Go语方)开发环境搭建与配置

安装完成后,可以打开终端,输入go,检测Golang SDK是否安装成功。

输入 go version

看到显示下图,即表示成功。

Golfing(Go语方)开发环境搭建与配置

环境变量配置

根据官网说明,需要配置环境变量,原文内容见截图:

Golfing(Go语方)开发环境搭建与配置

从文中内容可见(本人英文不是太好,如有错误,见笑了),默认安装后,路径为/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

Golfing(Go语方)开发环境搭建与配置

翻译:

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.


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

程序员的算法趣题

程序员的算法趣题

[ 日] 增井敏克 / 绝 云 / 人民邮电出版社 / 2017-7 / 55.00元

本书是一本解谜式的趣味算法书,从实际应用出发,通过趣味谜题的解谜过程,引导读者在愉悦中提升思维能力、掌握算法精髓。此外,本书作者在谜题解答上,通过算法的关键原理讲解,从思维细节入手,发掘启发性算法新解,并辅以Ruby、JavaScript等不同语言编写的源代码示例,使读者在算法思维与编程实践的分合之间,切实提高编程能力。 本书适合已经学习过排序、搜索等知名算法,并想要学习更多有趣算法以提升编程技巧......一起来看看 《程序员的算法趣题》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换