Golfing(Go語方)開發環境搭建與配置

來源:互聯網
上載者:User

官方說明,地址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.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.