加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

golang博客系统beego-blog编译及运行笔记

发布时间:2020-12-16 18:21:50 所属栏目:大数据 来源:网络整理
导读:golang博客系统beego-blog编译及运行笔记 beego-blog是go代码大师UlricQin基于beego创建的博客系统,今天拿过来编译运行,当成go入门的练习课程了。 代码来自[github.com/UlricQin/beego-blog] 。 其中安装方法写的太简单,本文实践之后做了一些补充。 贴一

golang博客系统beego-blog编译及运行笔记

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 gg.go:8:2: cannot find package "github.com/go-sql-driver/mysql" in any of: E:rungo1.7beta1srcgithub.comgo-sql-drivermysql (from $GOROOT)
        E:rungopathsrcgithub.comgo-sql-drivermysql (from $GOPATH) gcfg.go:4:2: cannot find package "github.com/qiniu/api.v6/conf" in any of: E:rungo1.7beta1srcgithub.comqiniuapi.v6conf (from $GOROOT)
        E:rungopathsrcgithub.comqiniuapi.v6conf (from $GOPATH) gqiniu.go:4:2: cannot find package "github.com/qiniu/api.v6/io" in any of: E:rungo1.7beta1srcgithub.comqiniuapi.v6io (from $GOROOT)
        E:rungopathsrcgithub.comqiniuapi.v6io (from $GOPATH) gqiniu.go:5:2: cannot find package "github.com/qiniu/api.v6/rs" in any of: E:rungo1.7beta1srcgithub.comqiniuapi.v6rs (from $GOROOT)
        E:rungopathsrcgithub.comqiniuapi.v6rs (from $GOPATH) gmarkdown.go:4:2: cannot find package "github.com/slene/blackfriday" in any of: E:rungo1.7beta1srcgithub.comsleneblackfriday (from $GOROOT)
        E:rungopathsrcgithub.comsleneblackfriday (from $GOPATH) controllersapi_controller.go:7:2: cannot find package "github.com/ulricqin/goutils/filetool" in any of: E:rungo1.7beta1srcgithub.comulricqingoutilsfiletool (from $GOROOT)
        E:rungopathsrcgithub.comulricqingoutilsfiletool (from $GOPATH) gg.go:9:2: cannot find package "github.com/ulricqin/goutils/logtool" in any of: E:rungo1.7beta1srcgithub.comulricqingoutilslogtool (from $GOROOT)
        E:rungopathsrcgithub.comulricqingoutilslogtool (from $GOPATH) controllersbase_controller.go:6:2: cannot find package "github.com/ulricqin/goutils/paginator" in any of: E:rungo1.7beta1srcgithub.comulricqingoutilspaginator (from $GOROOT)
        E:rungopathsrcgithub.comulricqingoutilspaginator (from $GOPATH) controllersapi_controller.go:8:2: cannot find package "github.com/ulricqin/goutils/strtool" in any of: E:rungo1.7beta1srcgithub.comulricqingoutilsstrtool (from $GOROOT)
        E:rungopathsrcgithub.comulricqingoutilsstrtool (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
gcache.go:9: cannot use blogCacheExpire (type int64) as type time.Duration in argument to Cache.Put
gcache.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,time.Duration(blogCacheExpire))

继续编译go build main.go,出现以下错误:

$ go build main.go
gg.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
controllersapi_controller.go:38: this.ServeJson undefined (type *ApiController has no field or method ServeJson) controllersapi_controller.go:83: this.ServeJson undefined (type *ApiController has no field or method ServeJson) controllersarticle_controller.go:18: this.TplNames undefined (type *ArticleController has no field or method TplNames) controllersarticle_controller.go:25: this.TplNames undefined (type *ArticleController has no field or method TplNames) controllersarticle_controller.go:84: this.TplNames undefined (type *ArticleController has no field or method TplNames) controllerscatalog_controller.go:23: this.TplNames undefined (type *CatalogController has no field or method TplNames) controllerscatalog_controller.go:41: this.TplNames undefined (type *CatalogController has no field or method TplNames) controllerslogin_controller.go:12: this.TplNames undefined (type *LoginController has no field or method TplNames) controllersmain_controller.go:17: this.TplNames undefined (type *MainController has no field or method TplNames) controllersmain_controller.go:36: this.TplNames undefined (type *MainController has no field or method TplNames) controllersmain_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的代码。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读