beego-blog是go代码大师UlricQin基于beego创建的博客系统,今天拿过来编译运行,当成go入门的练习课程了。
代码来自[github.com/UlricQin/beego-blog] 。
其中安装方法写的太简单,本文实践之后做了一些补充。
贴一下作者的安装说明:
install
mkdir -p $GOPATH/src/github.com/ulricqin
cd $GOPATH/src/github.com/ulricqin
git clone https://github.com/UlricQin/beego-blog.git
go get github.com/ulricqin/beego-blog/...
cd beego-blog && modify conf/app.conf
bee run
按照作者的方法,就差一步go build了:
cd beego-blog
$ go build main.go
g\g.go:8:2: cannot find package "github.com/go-sql-driver/mysql" in any of:
E:\run\go1.7beta1\src\github.com\go-sql-driver\mysql (from $GOROOT)
E:\run\gopath\src\github.com\go-sql-driver\mysql (from $GOPATH)
g\cfg.go:4:2: cannot find package "github.com/qiniu/api.v6/conf" in any of:
E:\run\go1.7beta1\src\github.com\qiniu\api.v6\conf (from $GOROOT)
E:\run\gopath\src\github.com\qiniu\api.v6\conf (from $GOPATH)
g\qiniu.go:4:2: cannot find package "github.com/qiniu/api.v6/io" in any of:
E:\run\go1.7beta1\src\github.com\qiniu\api.v6\io (from $GOROOT)
E:\run\gopath\src\github.com\qiniu\api.v6\io (from $GOPATH)
g\qiniu.go:5:2: cannot find package "github.com/qiniu/api.v6/rs" in any of:
E:\run\go1.7beta1\src\github.com\qiniu\api.v6\rs (from $GOROOT)
E:\run\gopath\src\github.com\qiniu\api.v6\rs (from $GOPATH)
g\markdown.go:4:2: cannot find package "github.com/slene/blackfriday" in any of:
E:\run\go1.7beta1\src\github.com\slene\blackfriday (from $GOROOT)
E:\run\gopath\src\github.com\slene\blackfriday (from $GOPATH)
controllers\api_controller.go:7:2: cannot find package "github.com/ulricqin/goutils/filetool" in any of:
E:\run\go1.7beta1\src\github.com\ulricqin\goutils\filetool (from $GOROOT)
E:\run\gopath\src\github.com\ulricqin\goutils\filetool (from $GOPATH)
g\g.go:9:2: cannot find package "github.com/ulricqin/goutils/logtool" in any of:
E:\run\go1.7beta1\src\github.com\ulricqin\goutils\logtool (from $GOROOT)
E:\run\gopath\src\github.com\ulricqin\goutils\logtool (from $GOPATH)
controllers\base_controller.go:6:2: cannot find package "github.com/ulricqin/goutils/paginator" in any of:
E:\run\go1.7beta1\src\github.com\ulricqin\goutils\paginator (from $GOROOT)
E:\run\gopath\src\github.com\ulricqin\goutils\paginator (from $GOPATH)
controllers\api_controller.go:8:2: cannot find package "github.com/ulricqin/goutils/strtool" in any of:
E:\run\go1.7beta1\src\github.com\ulricqin\goutils\strtool (from $GOROOT)
E:\run\gopath\src\github.com\ulricqin\goutils\strtool (from $GOPATH)
显示没有这些依赖包,随后依次安装:
go get github.com/astaxie/beego
go get github.com/go-sql-driver/mysql
go get github.com/qiniu/api.v6
go get github.com/slene/blackfriday
go get github.com/ulricqin/goutils/filetool
go get github.com/ulricqin/goutils/paginator
go get github.com/ulricqin/goutils/logtool
go get github.com/ulricqin/goutils/strtool
安装后继续go build,然后又出现错误:
$ go build main.go
# github.com/ulricqin/beego-blog/g
g\cache.go:9: cannot use blogCacheExpire (type int64) as type time.Duration in argument to Cache.Put
g\cache.go:13: cannot use catalogCacheExpire (type int64) as type time.Duration in argument to Cache.Put
是blogCacheExpire 类型问题,blogCacheExpire 申明为int64,time.Duration也是int64,但是在go编译时不认为是同一种类型,解决方法是调用blogCacheExpire 的地方强行转换为time.Duration()类型:
vim g/g.go
//在第2行增加
import (
"time"
)
//将9,13行
return Cache.Put(blogPrefix+key, val, blogCacheExpire)
//修改为
return Cache.Put(blogPrefix+key, val, time.Duration(blogCacheExpire))
继续编译go build main.go,出现以下错误:
$ go build main.go
g\g.go:45: undefined: orm.DR_MySQL
问题是没找到 orm.DR_MySQL,解决方法是去$GOPATH/src/github.com/astaxie/beego下找到orm.go,发现里面并没有定义 DR_MySQL,但是注释里有使用方法use sample:
// Simple Usage
//
// package main
//
// import (
// "fmt"
// "github.com/astaxie/beego/orm"
// _ "github.com/go-sql-driver/mysql" // import your used driver
// )
//
// // Model Struct
// type User struct {
// Id int `orm:"auto"`
// Name string `orm:"size(100)"`
// }
//
// func init() {
// orm.RegisterDataBase("default", "mysql", "root:root@/my_db?charset=utf8", 30)
// }
//
// func main() {
// o := orm.NewOrm()
// user := User{Name: "slene"}
// // insert
// id, err := o.Insert(&user)
// // update
// user.Name = "astaxie"
// num, err := o.Update(&user)
// // read one
// u := User{Id: user.Id}
// err = o.Read(&u)
// // delete
// num, err = o.Delete(&u)
// }
//
由此推断UlricQin的g/g.go中使用的orm版本可能与官方的版本有点差别,官方说明代码中不需要注册驱动,所以改动:
vim g/g/go
// :45<enter> 45行左右进行注释
//orm.RegisterDriver("mysql", orm.DR_MySQL)
继续编译go build maim.go,出现以下错误:
$ go build main.go
# github.com/ulricqin/beego-blog/controllers
controllers\api_controller.go:38: this.ServeJson undefined (type *ApiController has no field or method ServeJson)
controllers\api_controller.go:83: this.ServeJson undefined (type *ApiController has no field or method ServeJson)
controllers\article_controller.go:18: this.TplNames undefined (type *ArticleController has no field or method TplNames)
controllers\article_controller.go:25: this.TplNames undefined (type *ArticleController has no field or method TplNames)
controllers\article_controller.go:84: this.TplNames undefined (type *ArticleController has no field or method TplNames)
controllers\catalog_controller.go:23: this.TplNames undefined (type *CatalogController has no field or method TplNames)
controllers\catalog_controller.go:41: this.TplNames undefined (type *CatalogController has no field or method TplNames)
controllers\login_controller.go:12: this.TplNames undefined (type *LoginController has no field or method TplNames)
controllers\main_controller.go:17: this.TplNames undefined (type *MainController has no field or method TplNames)
controllers\main_controller.go:36: this.TplNames undefined (type *MainController has no field or method TplNames)
controllers\main_controller.go:36: too many errors
拿beego-blog的controller与beego官网的controller比较以下,发现成员变量和方法TplNames,ServeJson与官方不一样,分别替换为TplName,ServeJSON后,编译通过。
$ ./main.exe run
2016/12/14 20:03:44 [I] [asm_amd64.s:2059] http server Running on http://:8999
[ORM]2016/12/14 20:04:12 -[Queries/default] - [ OK / db.Query / 4.0ms] - [SELECT T0.`id` FROM `bb_catalog` T0 ORDER BY T0.`display_order` DESC LIMIT 1000]
[beego] 2016/12/14 - 20:04:12 | 127.0.0.1| 200 | 6.0003ms| match| GET / r:/
[beego] 2016/12/14 - 20:04:12 | 127.0.0.1| 200 | 2.0001ms| match| GET /static/css/g.css
[beego] 2016/12/14 - 20:04:12 | 127.0.0.1| 200 | 7.0004ms| match| GET /static/css/ee22d.css
[beego] 2016/12/14 - 20:04:12 | 127.0.0.1| 200 | 11.0007ms| match| GET /static/images/guy.jpg
[beego] 2016/12/14 - 20:04:12 | 127.0.0.1| 200 | 0s| match| GET /static/images/social/weibo.png
[beego] 2016/12/14 - 20:04:12 | 127.0.0.1| 200 | 0s| match| GET /static/images/social/facebook.png
[beego] 2016/12/14 - 20:04:12 | 127.0.0.1| 200 | 0s| match| GET /static/images/social/twitter.png
[beego] 2016/12/14 - 20:04:12 | 127.0.0.1| 200 | 1.0001ms| match| GET /static/images/social/github.png
[beego] 2016/12/14 - 20:04:12 | 127.0.0.1| 200 | 1.0001ms| match| GET /static/images/code.png
[beego] 2016/12/14 - 20:04:12 | 127.0.0.1| 200 | 0s| match| GET /static/favicon.ico
[beego] 2016/12/14 - 20:04:25 | 127.0.0.1| 302 | 0s| match| GET /me r:/me
[beego] 2016/12/14 - 20:04:25 | 127.0.0.1| 200 | 1ms| match| GET /login r:/login
打开http://127.0.0.1:8889 验证运行成功。
行了,祝大家学习愉快。
感谢UlricQin的代码。