mac系統下搭建go語言環境

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

1,首先查看是否安裝go,或者安裝版本

yishiyaonie:GO liuhanlin$ go versiongo version go1.5.1 darwin/amd64

記住版本不能過低,比如1.2,在以後的編譯項目過程中可能會帶來很多很多麻煩。切記。

2,配置go的開發環境

  • 首先要確定你的開發目錄,也就是go項目的代碼存放位置
mkdir -p /Users/liuhanlin/GO
  • 其次需要vi ~/.bash_profile ,有就直接開啟,沒有就建立一個新的。
export GOPATH=/Users/liuhanlin/GOexport GOBIN=$GOPATH/binexport PATH=$PATH:$GOBIN

加入如下環境變數控制,最後儲存

source ~/.bash_profile 

3,檢查搭建情況

yishiyaonie:GO liuhanlin$ go envGOARCH="amd64"GOBIN="/Users/liuhanlin/GO/bin"GOEXE=""GOHOSTARCH="amd64"GOHOSTOS="darwin"GOOS="darwin"GOPATH="/Users/liuhanlin/GO"GORACE=""GOROOT="/usr/local/go"GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"GO15VENDOREXPERIMENT=""CC="clang"GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common"CXX="clang++"CGO_ENABLED="1"

gobin檔案夾是用來存放go項目編譯的二進位檔案的

4,環境已經配置好,接下來搭建個工程玩玩

yishiyaonie:~ liuhanlin$ go get -u github.com/qor/qor-examplepackage golang.org/x/net/html: unrecognized import path "golang.org/x/net/html"package golang.org/x/image/bmp: unrecognized import path "golang.org/x/image/bmp"package golang.org/x/image/tiff: unrecognized import path "golang.org/x/image/tiff"package golang.org/x/net/context: unrecognized import path "golang.org/x/net/context"

我們發現有幾個依賴的庫沒有下載下來,導致go get 沒能正常的build編譯。
解決方案有三種
1,翻牆,直到能夠下載golang.org/x下的檔案位置,建議不採用,因為我嘗試過各種方式。
2,去gopm上去下載,手動下載的是zip包,但是在編譯的時候會報錯誤,具體錯誤應該是跟git有關的。
3,最簡單,最快捷的方式就是直接到git上面去git clone。接下來採用git clone的方式。

mkdir -p golang.org/x/cd /Users/liuhanlin/GO/src/golang.org/xgit clone https://github.com/golang/net.gitgit clone https://github.com/golang/image.git

5,重新運行下go get

go get -u github.com/qor/qor-exampleyishiyaonie:GO liuhanlin$ lsbin pkg src

我們看到go get 在下載包的同時,也編譯了檔案,但是此時的檔案是有問題的,當真正啟動並執行時候是有錯誤的。可能就是所謂的動態編譯何靜態編譯的問題吧。

6,接下來需要建立資料庫

登入雲主機liuhanlin$ ssh -i ~/.ssh/vm1-ssh-key root@ip登入mysqlmysql -u root -p輸入:GRANT ALL ON *.* TO username@'%' IDENTIFIED BY 'password' WITH GRANT OPTION; 退出vi /etc/mysql/my.cnfbind-address = 127.0.0.1改為:bind-address = 0.0.0.0sudo /etc/init.d/mysql restart然後訪問下mysql -h ip -u root -p

如果成功代表ok

7,運行go cms 項目

liuhanlin$ cd $GOPATH/src/github.com/qor/qor-exampleliuhanlin$ go run main.goFailed to find configuration config/database.yml, using example file config/database.example.ymlpanic: dial tcp 127.0.0.1:3306: getsockopt: connection refusedgoroutine 1 [running]:github.com/qor/qor-example/db.init.1()    /Users/liuhanlin/GO/src/github.com/qor/qor-example/db/db.go:44 +0x4c1github.com/qor/qor-example/db.init()    /Users/liuhanlin/GO/src/github.com/qor/qor-example/db/db.go:46 +0x6dgithub.com/qor/qor-example/app/models.init()    /Users/liuhanlin/GO/src/github.com/qor/qor-example/app/models/user.go:21 +0x74github.com/qor/qor-example/config/admin.init()    /Users/liuhanlin/GO/src/github.com/qor/qor-example/config/admin/worker.go:133 +0x6cmain.init()    /Users/liuhanlin/GO/src/github.com/qor/qor-example/main.go:29 +0x4agoroutine 17 [syscall, locked to thread]:runtime.goexit()    /usr/local/go/src/runtime/asm_amd64.s:1696 +0x1goroutine 20 [chan receive]:database/sql.(*DB).connectionOpener(0xc820282f00)    /usr/local/go/src/database/sql/sql.go:634 +0x45created by database/sql.Open    /usr/local/go/src/database/sql/sql.go:481 +0x336exit status 2

哇,報錯了。很顯然啊,這是資料庫的連結報錯,或許是因為密碼不對,或者資料庫不存在,或者資料庫網路不通都有可能。所以接下來。

yishiyaonie:qor-example liuhanlin$ vi config/database.example.yml加入訪問密碼yishiyaonie:qor-example liuhanlin$ cat config/database.example.yml db:  adapter: mysql  name: qor_example  user: root  password: 。。。

8, 修改遠端連線資料庫代碼

fmt.Sprintf("%v:%v@tcp(ip:3306)/%v", dbConfig.User, dbConfig.Password, dbConfig.Name))

9,運行go項目

yishiyaonie:qor-example liuhanlin$ go run main.go Failed to find configuration config/database.yml, using example file config/database.example.yml[info] replacing callback `gorm:delete` from /Users/liuhanlin/GO/src/github.com/qor/publish/publish.go:144Failed to create unique index for translations key & locale, got: Error 1170: BLOB/TEXT column 'key' used in key specification without a key length(Error 1170: BLOB/TEXT column 'key' used in key specification without a key length) [2016-05-29 18:00:07]  [GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production. - using env:   export GIN_MODE=release - using code:  gin.SetMode(gin.ReleaseMode)[GIN-debug] GET    /                         --> github.com/qor/qor-example/app/controllers.HomeIndex (3 handlers)[GIN-debug] GET    /products/:code           --> github.com/qor/qor-example/app/controllers.ProductShow (3 handlers)Listening on: 7000[GIN] 2016/05/29 - 18:00:48 | 200 |  282.869157ms | ::1 |   GET     /

10,訪問本地環境

http://localhost:7000/admin

ok,花了周末兩天時間學了好多東西,希望能幫到同樣需要協助的人。

相關文章

聯繫我們

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