Ubuntu下搭建Golang開發環境

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

Golang安裝及配置

下載安裝檔案,當前最新版本為1.4.2,go1.4.2.linux-amd64.tar.gz 。

將安裝檔案解壓至指定目錄:

1 # sudo tar -zxvf go1.4.2.linux-amd64.tar.gz -C /usr/local/

編輯 /etc/profile 檔案添加環境變數:

export GOROOT=/usr/local/goexport GOBIN=$GOROOT/binexport PATH=$PATH:$GOBINexport GOPATH=$HOME/goproj

重新編譯 /etc/profile 檔案,並嘗試查看GO版本:

1 # source /etc/profile2 # go version

Golang交叉編譯

建立 hello.go 檔案,內容如下:

1 package main2 3 import (4     "fmt"5 )6 7 func main() {8     fmt.Println("Hello Golang!")9 }

Go安裝包中只打包了相應平台下的編譯工具,為實現交叉編譯,需要手動產生其他平台的編譯工具。下面將產生 windows 64位編譯器,開啟 /usr/local/go/src/ ,執行如下代碼,GOOS表示目標系統類別型,GOARCH說明目標系統是32位(386)或64位(amd64):

1 # sudo GOOS=windows GOARCH=amd64 ./make.bash

命令執行成功後,會在 /usr/local/go/pkg/ 下產生相應平台的編譯工具。

接下來在LiteIDE中選擇相應的目標平台,編譯GO程式碼,結果如下:

最後附上各平台GOOS和GOARCH參考:

GOOS GOARCH OS
linux 386/amd64/arm >= Linux2.6
darwin 386/amd64 OS X (Snow Leopard + Lion)
freebsd 386/amd64 >= FreeBSD 7
windows 386/amd64 >= Windows 2000
相關文章

聯繫我們

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