GO 語言的GOROOT 和GOPATH

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

Mac下使用Google官方的Go語言安裝包:https://code.google.com/p/go/downloads/list 安裝的Go,會自動把 /usr/local/go/bin 目錄加入PATH中。這樣我們直接在控制台就可以執行go語言的一些命令。

http://golang.org/cmd/go/#hdr-GOPATH_environment_variablehttp://www.cnblogs.com/ghj1976/archive/2013/01/16/2863142.html

Go的二進位編譯包假設你把Go安裝在 /usr/local/go (或者Window是 c:\Go)目錄下。當然你也可以安裝在其他目錄下,不過這時候你就需要設定GOROOT環境變數了。 http://golang.org/doc/install#install 例如,你如果安裝Go在你的/usr/local/go目錄下,你應該$HOME/.profile檔案增加下面設定。

export GOROOT=/usr/local/goexport GOPATH=$PATH:$GOROOT/bin

執行:source .bash_profile (即時生效)

Window下則是:

Under Windows, you may set environment variables through the "Environment Variables" button on the "Advanced" tab of the "System" control panel. Some versions of Windows provide this control panel through the "Advanced System Settings" option inside the "System" control panel.

比如我的Mac本,其實我沒有設定GOROOT,但是通過 go env 可以得到GOROOT的目錄是:/usr/local/go

我猜測這應該是沒有設定時的預設設定。如果有設定,會覆蓋。

GOPATH

GOPATH的作用是告訴Go 命令和其他相關工具,在哪裡去找到安裝在你系統上的Go包。

GOPATH是一個路徑的列表,一個典型的GOPATH設定如下,類似PATH的設定,Win下用分號分割:

GOPATH=/home/user/ext:/home/user/mygo 

每一個列表中的路徑是一個工作區的位置。每個工作區都有源檔案、相關包的對象、執行檔案。http://golang.org/doc/code.html

下面是一個建立工作區的步驟:

建立 $HOME/mygo 目錄和作為原始碼的 src 目錄。

$ mkdir -p $HOME/mygo/src # create a place to put source code 

下一步就是設定 GOPATH,另外你應該把 這個目錄下的bin目錄放在 PATH 環境變數,這樣你就可以直接在命令列執行而不用給出完整目錄。

export GOPATH=$HOME/mygo export PATH=$PATH:$HOME/mygo/bin

GOPATH 必須設定編譯和安裝包,即使用標準的Go分類樹,類似如下:

GOPATH=/home/user/gocode/home/user/gocode/    src/        foo/            bar/               (go code in package bar)                x.go            quux/              (go code in package main)                y.go    bin/        quux                   (installed command)    pkg/        linux_amd64/            foo/                bar.a          (installed package object)

http://golang.org/cmd/go/#hdr-GOPATH_environment_variable

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.