mac系统下搭建go语言环境

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

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

记住版本不能过低,比如1.2,在以后的编译项目过程中可能会带来很多很多麻烦。切记。

2,配置go的开发环境

  • 首先要确定你的开发目录,也就是go项目的代码存放位置
mkdir -p /Users/liuhanlin/GO
  • 其次需要vi ~/.bash_profile ,有就直接打开,没有就创建一个新的。
export GOPATH=/Users/liuhanlin/GO
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN

加入如下环境变量控制,最后保存

source ~/.bash_profile 

3,检查搭建情况

yishiyaonie:GO liuhanlin$ go env
GOARCH="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-example
package 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/x
git clone https://github.com/golang/net.git
git clone https://github.com/golang/image.git

5,重新运行下go get

go get -u github.com/qor/qor-example
yishiyaonie:GO liuhanlin$ ls
bin pkg src

我们看到go get 在下载包的同时,也编译了文件,但是此时的文件是有问题的,当真正运行的时候是有错误的。可能就是所谓的动态编译何静态编译的问题吧。

6,接下来需要创建数据库

登录云主机
liuhanlin$ ssh -i ~/.ssh/vm1-ssh-key [email protected]
登录mysql
mysql -u root -p
输入:
GRANT ALL ON *.* TO [email protected]'%' IDENTIFIED BY 'password' WITH GRANT OPTION; 
退出
vi /etc/mysql/my.cnf
bind-address = 127.0.0.1
改为:
bind-address = 0.0.0.0
sudo /etc/init.d/mysql restart
然后访问下
mysql -h ip -u root -p

如果成功代表ok

7,运行go cms 项目

liuhanlin$ cd $GOPATH/src/github.com/qor/qor-example

liuhanlin$ go run main.go
Failed to find configuration config/database.yml, using example file config/database.example.yml
panic: dial tcp 127.0.0.1:3306: getsockopt: connection refused

goroutine 1 [running]:
github.com/qor/qor-example/db.init.1()
    /Users/liuhanlin/GO/src/github.com/qor/qor-example/db/db.go:44 +0x4c1
github.com/qor/qor-example/db.init()
    /Users/liuhanlin/GO/src/github.com/qor/qor-example/db/db.go:46 +0x6d
github.com/qor/qor-example/app/models.init()
    /Users/liuhanlin/GO/src/github.com/qor/qor-example/app/models/user.go:21 +0x74
github.com/qor/qor-example/config/admin.init()
    /Users/liuhanlin/GO/src/github.com/qor/qor-example/config/admin/worker.go:133 +0x6c
main.init()
    /Users/liuhanlin/GO/src/github.com/qor/qor-example/main.go:29 +0x4a

goroutine 17 [syscall, locked to thread]:
runtime.goexit()
    /usr/local/go/src/runtime/asm_amd64.s:1696 +0x1

goroutine 20 [chan receive]:
database/sql.(*DB).connectionOpener(0xc820282f00)
    /usr/local/go/src/database/sql/sql.go:634 +0x45
created by database/sql.Open
    /usr/local/go/src/database/sql/sql.go:481 +0x336
exit 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:144
Failed 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,花了周末两天时间学了好多东西,希望能帮到同样需要帮助的人。

阅读更多

更多精彩内容