go 环境搭建及beego开源框架样例测试

写在前面的话

每篇一句

见贤思齐焉,见不贤而内自省也。

go 安装

1
2
3
cd /data/
wget https://storage.googleapis.com/golang/go1.9.1.linux-amd64.tar.gz
tar -C /usr/local -xzf go1.9.1.linux-amd64.tar.gz

/root/.bashrc 文件增加go环境变量配置,针对root用户配置环境变量。如果想所有用户使用可追加在/etc/profile。

1
2
3
export GOROOT=/usr/local/go
export GOPATH=/data/mygoworkspace
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

使环境变量立即生效

1
source /root/.bashrc

验证

1
2
[root@master ~]# go version
go version go1.9.1 linux/amd64

查看所有go 环境变量可使用go env

beego 安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
go get -u github.com/astaxie/beego
go get -u github.com/beego/bee
exec $SHELL
[root@docker_host mygoworkspace]# bee version
______
| ___ \
| |_/ / ___ ___
| ___ \ / _ \ / _ \
| |_/ /| __/| __/
\____/ \___| \___| v1.9.1
├── Beego : 1.9.0
├── GoVersion : go1.9.1
├── GOOS : linux
├── GOARCH : amd64
├── NumCPU : 1
├── GOPATH : /data/mygoworkspace
├── GOROOT : /usr/local/go
├── Compiler : gc
└── Date : Friday, 1 Dec 2017
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@docker_host mygoworkspace]# pwd
/data/mygoworkspace
[root@docker_host mygoworkspace]# tree -L 4
.
├── bin
│   └── bee
├── pkg
│   └── linux_amd64
│   └── github.com
│   ├── astaxie
│   └── beego
└── src
└── github.com
├── astaxie
│   └── beego
└── beego
└── bee
12 directories, 1 file

快速建测试应用检测运行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
[root@docker_host mygoworkspace]# cd $GOPATH
[root@docker_host mygoworkspace]# cd src/
[root@docker_host src]# bee new hello
______
______
| ___ \
| |_/ / ___ ___
| ___ \ / _ \ / _ \
| |_/ /| __/| __/
\____/ \___| \___| v1.9.1
2017/12/01 22:08:50 INFO ▶ 0001 Creating application...
create /data/mygoworkspace/src/hello/
create /data/mygoworkspace/src/hello/conf/
create /data/mygoworkspace/src/hello/controllers/
create /data/mygoworkspace/src/hello/models/
create /data/mygoworkspace/src/hello/routers/
create /data/mygoworkspace/src/hello/tests/
create /data/mygoworkspace/src/hello/static/
create /data/mygoworkspace/src/hello/static/js/
create /data/mygoworkspace/src/hello/static/css/
create /data/mygoworkspace/src/hello/static/img/
create /data/mygoworkspace/src/hello/views/
create /data/mygoworkspace/src/hello/conf/app.conf
create /data/mygoworkspace/src/hello/controllers/default.go
create /data/mygoworkspace/src/hello/views/index.tpl
create /data/mygoworkspace/src/hello/routers/router.go
create /data/mygoworkspace/src/hello/tests/default_test.go
create /data/mygoworkspace/src/hello/main.go
2017/12/01 22:08:50 SUCCESS ▶ 0002 New application successfully created!
[root@docker_host src]# cd hello/
[root@docker_host hello]# bee run hello
______
| ___ \
| |_/ / ___ ___
| ___ \ / _ \ / _ \
| |_/ /| __/| __/
\____/ \___| \___| v1.9.1
2017/12/01 22:10:17 INFO ▶ 0001 Using 'hello' as 'appname'
2017/12/01 22:10:17 INFO ▶ 0002 Initializing watcher...
2017/12/01 22:10:18 SUCCESS ▶ 0003 Built Successfully!
2017/12/01 22:10:18 INFO ▶ 0004 Restarting 'hello'...
2017/12/01 22:10:18 SUCCESS ▶ 0005 './hello' is running...
2017/12/01 22:10:18 [I] [asm_amd64.s:2337] http server Running on http://:8080

如果想换其他端口,可以更改当前目录下conf/app.conf

beego提供的样例应用运行测试

样例代码获取

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@docker_host hello]# cd $GOPATH/src
[root@docker_host src]# pwd
/data/mygoworkspace/src
[root@docker_host src]# git clone https://github.com/beego/samples
Cloning into 'samples'...
remote: Counting objects: 269, done.
remote: Total 269 (delta 0), reused 0 (delta 0), pack-reused 269
Receiving objects: 100% (269/269), 333.56 KiB | 145.00 KiB/s, done.
Resolving deltas: 100% (87/87), done.
[root@docker_host src]# cd data/mygoworkspace/src/github.com/beego
[root@docker_host beego]# go get github.com/beego/samples
Cloning into 'samples'...
remote: Counting objects: 269, done.
remote: Total 269 (delta 0), reused 0 (delta 0), pack-reused 269
Receiving objects: 100% (269/269), 333.56 KiB | 40.00 KiB/s, done.
Resolving deltas: 100% (87/87), done.

WebIM web在线聊天应用测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@docker_host samples]# cd $GOPATH/src/samples/WebIM
[root@docker_host WebIM]# go get github.com/gorilla/websocket
[root@docker_host WebIM]# go get github.com/beego/i18n
[root@docker_host WebIM]# bee run
______
| ___ \
| |_/ / ___ ___
| ___ \ / _ \ / _ \
| |_/ /| __/| __/
\____/ \___| \___| v1.9.1
2017/12/01 22:47:17 INFO ▶ 0001 Using 'WebIM' as 'appname'
2017/12/01 22:47:17 INFO ▶ 0002 Initializing watcher...
github.com/beego/samples/WebIM/models
github.com/beego/samples/WebIM/controllers
github.com/beego/samples/WebIM/routers
_/data/mygoworkspace/src/samples/WebIM
2017/12/01 22:47:18 SUCCESS ▶ 0003 Built Successfully!
2017/12/01 22:47:18 INFO ▶ 0004 Restarting 'WebIM'...
2017/12/01 22:47:18 SUCCESS ▶ 0005 './WebIM' is running...
2017/12/01 22:47:18 [D] [app.go:32] Loading language: en-US
2017/12/01 22:47:18 [D] [app.go:32] Loading language: zh-CN
2017/12/01 22:47:18 [I] [WebIM.go:29] Web IM 0.1.1.0227
2017/12/01 22:47:18 [I] [asm_amd64.s:2337] http server Running on http://:8080

效果图

todo list 待办清单应用测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@docker_host samples]# cd todo/
[root@docker_host todo]# bee run
______
| ___ \
| |_/ / ___ ___
| ___ \ / _ \ / _ \
| |_/ /| __/| __/
\____/ \___| \___| v1.9.1
2017/12/01 22:49:32 INFO ▶ 0001 Using 'todo' as 'appname'
2017/12/01 22:49:32 INFO ▶ 0002 Initializing watcher...
2017/12/01 22:49:33 SUCCESS ▶ 0003 Built Successfully!
2017/12/01 22:49:33 INFO ▶ 0004 Restarting 'todo'...
2017/12/01 22:49:33 SUCCESS ▶ 0005 './todo' is running...
2017/12/01 22:49:33 [I] [asm_amd64.s:2337] http server Running on http://:8080

api应用测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@docker_host samples]# cd shorturl/
[root@docker_host shorturl]# bee run
______
| ___ \
| |_/ / ___ ___
| ___ \ / _ \ / _ \
| |_/ /| __/| __/
\____/ \___| \___| v1.9.1
2017/12/01 22:50:30 INFO ▶ 0001 Using 'shorturl' as 'appname'
2017/12/01 22:50:30 INFO ▶ 0002 Initializing watcher...
github.com/astaxie/beego/cache
github.com/beego/samples/shorturl/models
github.com/beego/samples/shorturl/controllers
_/data/mygoworkspace/src/samples/shorturl
2017/12/01 22:50:31 SUCCESS ▶ 0003 Built Successfully!
2017/12/01 22:50:31 INFO ▶ 0004 Restarting 'shorturl'...
2017/12/01 22:50:31 SUCCESS ▶ 0005 './shorturl' is running...
2017/12/01 22:50:31 [I] [asm_amd64.s:2337] http server Running on http://:8080
2017/12/01 22:50:45 [I] [short.go:29] http://www.baidu.com
2017/12/01 22:50:45 [I] [short.go:32] 1bbd7af9c11579c1e245811615e512f3
100000001
2017/12/01 22:50:45 [D] [server.go:2619] | 127.0.0.1| 200 | 202.832µs| match| GET /v1/shorten/ r:/v1/shorten
2017/12/01 22:50:54 [D] [server.go:2619] | 127.0.0.1| 200 | 46.886µs| match| GET /v1/expand/ r:/v1/expand
1
2
3
4
5
6
7
8
9
[root@docker_host beego]# curl http://localhost:8080/v1/shorten/?longurl=http://www.baidu.com
{
"UrlShort": "5laZF",
"UrlLong": "http://www.baidu.com"
}[root@docker_host beego]# curl http://localhost:8080/v1/expand/?shorturl=5laZF
{
"UrlShort": "5laZF",
"UrlLong": "http://www.baidu.com"
}

BeeGo开源框架官网

----纸上得来终觉浅绝知此事要躬行----
最好的赞赏是您的阅读!
0%