1.0 這方面的資料在網站上確實不多html
2.0 在用bee工具建立一個go項目後,接下來咱們有2件事要作了,固然以前一隻以爲GO的IDE實在不知道選着那個,由於在Mac電腦上開發,又不支持文件建立因此有點麻煩mysql
最終仍是肯定用sublime來開發。sublime自己集合了命令行插件這樣開發起來就不用在幾個命令行窗口跳轉git
3.0 安裝好sublime後用快捷鍵進入sublime pagecontrol 或按shift+command+p 打開 輸入GOSUBLIME:rungocommand 這樣就能夠Mac 命令建立文件 在這個平臺上快速建立了github
[ `ls` | done: 130.738533ms ] app conf controllers main.go models routers static tests views [ `cd views` | done ] [ /website/apple/apps/src/app/ ] # [ `ls` | done: 207.172909ms ] category.html index.html index.tpl [ `open index.html` | done: 380.637835ms ] [ `open -e index.html` | done: 526.056184ms ] [ `open -a index.html` | done: 1.356006514s ] Unable to find application named 'index.html' exit status 1 [ `sublime index.html` | done: 142.359053ms ] /bin/bash: sublime: command not found exit status 127 [ `ln -s /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl /usr/local/bin/sublime` | done: 205.678885ms ] [ `sublime index.html` | done: 361.883007ms ] [ /website/apple/apps/src/app/views/ ] #
3.0 好了,開始說beego 使用orm連接以及建立mysql數據庫web
3.1 直接上代碼分3個文件mode.go,main.go,home.go這樣作是爲了實現業務分離 ,先來home.go是mvc中controllersql
package controllers import ( "github.com/astaxie/beego" ) type MainController struct { beego.Controller } func (this *MainController) Get() { //定義首頁模板 this.TplNames = "index.html" }
3.2 mode.go 主要是負責數據庫處理(mvc中的mode層)數據庫
package models import ( "github.com/astaxie/beego/orm" "time" ) type Store struct { Id int64 Title string Created time.Time `orm:"index"` Views int64 `orm:"index"` TopicTime time.Time `orm:"index"` TopicCount int64 TopicLastUserId int64 } type Customer struct { Id int64 Uid int64 Title string Content string `orm:"size(5000)"` Attachment string Created time.Time `orm:"index"` Updated time.Time `orm:"index"` Views int64 `orm:"index"` Author string ReplyTime time.Time `orm:"index"` ReplyCount int64 ReplyLastUserId int64 } func RegisterDB() { //註冊 model orm.RegisterModel(new(Store), new(Customer)) //註冊驅動 orm.RegisterDriver("mysql", orm.DR_MySQL) //註冊默認數據庫 orm.RegisterDataBase("default", "mysql", "root:@/app?charset=utf8")//密碼爲空格式 }
3.3 再一個就是main.go 負責在運行時鏈接數據庫根據模型建立數據庫表bash
package main import ( "app/models" _ "app/routers" "github.com/astaxie/beego" "github.com/astaxie/beego/orm" _"github.com/go-sql-driver/mysql" ) //引入數據模型 func init() { // 註冊數據庫 models.RegisterDB() } func main() { // 開啓 ORM 調試模式 orm.Debug = true // 自動建表 orm.RunSyncdb("default", false, true) // 運行時 beego.Run() }
3.4 吐槽一下,感受go的學習資料真的不多,成系統的項目學習資料就更少了,何時支持安卓啊!mvc