mac 安裝和運行go的helloword

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

哈哈,好激動,又接觸到一門強大的語言,簡單記錄下在mac上運行第一個程式遇到的問題。

Hello Word 樣本,pkg安裝包: wget https://storage.googleapis.com/golang/go1.2.2.darwin-amd64-osx10.8.pkg,下載後,需要設定環境變數才ok哦,如,我的

bash_profile檔案,

GOARCH="386"  # 386是來自 uname -p 擷取的哦

GOBIN=""

GOCHAR="6"

GOEXE=""

GOHOSTARCH="386" # 386是來自 uname -p 擷取的哦

GOHOSTOS="darwin"

GOOS="darwin"

GOPATH=""

GORACE=""

GOROOT="/usr/local/go"

GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"

TERM="dumb"

CC="clang"

GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fno-common"

CXX="clang++"

CGO_ENABLED="1"

source bash_profile即可;

如果在build時出現類似下面的錯誤(go build runtime: darwin/amd64 must be bootstrapped using make.bash),可以嘗試如下命令,再次編譯:
$ GOOS=darwin GOARCH=amd64 go build -x -o testport_linux hello.go 
WORK=/var/folders/vp/f5r3wgld1w133srlbyd4190m0000gn/T/go-build683397936
mkdir -p $WORK/command-line-arguments/_obj/
cd /Users/jake/rdd/golang
/usr/local/go/pkg/tool/darwin_amd64/6g -o $WORK/command-line-arguments/_obj/_go_.6 -p command-line-arguments -complete -D _/Users/jake/rdd/golang -I $WORK ./hello.go
/usr/local/go/pkg/tool/darwin_amd64/pack grcP $WORK $WORK/command-line-arguments.a $WORK/command-line-arguments/_obj/_go_.6
cd .
/usr/local/go/pkg/tool/darwin_amd64/6l -o testport_linux -L $WORK $WORK/command-line-arguments.a
我們發現Go實際上用的是$GOROOT/pkg/tool/linux_amd64下的6g(編譯器)和6l(連結器)來完成整個編譯過程的,看到6g 和6l所在目錄名為linux_amd64,我們可以大膽猜測編譯darwin/amd64 go程式應該使用的是$GOROOT/pkg/tool/darwin_amd64下的工具。不過在我在$GOROOT/pkg/tool下沒有發現 darwin_amd64目錄,也就是說我們通過安裝包安裝的Go僅內建了for linux_amd64的編譯工具,要想交叉編譯出for darwin_amd64的程式,我們需要通過make.bash來手工編譯出這些工具。
根據make.bash的用法說明,我們來嘗試構建一下:


cd $GOROOT/src
sudo GOOS=darwin GOARCH=amd64 ./make.bash


編譯後,我們再來試試編譯for darwin_amd64的程式:


$ GOOS=darwin GOARCH=amd64 go build -x -o test_darwin hello.go


此時,如果在目前的目錄產生了test_darwin檔案,那就執行,看下久違的hello world。那就修改環境變數:
 $ export GOARCH=amd64


聯繫我們

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