package導入
$ go get github.com/jinzhu/gorm
$ go get github.com/go-sql-driver/mysql
conf/app.conf
db.user = hoge db.password = fuga db.host = localhost db.port = 3306 db.name = test db.protocol = tcp
app/models/gorm.go
package models import ( "github.com/revel/revel" "github.com/jinzhu/gorm" "strings" "time" "fmt" _"github.com/go-sql-driver/mysql" ) var DB **gorm.DB func InitDB(){ db, err := gorm.Open("mysql", getConnectionString()) if err != nil { revel.ERROR.Println("FATAL", err) panic(err) } db.DB() DB = &db } type Model struct { gorm.Model ID unit `gorm:"primary_key"` CreatedAt time.Time UpdatedAt time.Time DeletedAt *time.Time } type Validator interface { IsStatisfied(interface{}) bool DefaultMessage() String } func getParamString(param string, defaultValue string) string { p, found := revel.Config.String(param) if !found { if defaultValue == "" { revel.ERROR.Fatal("Could not find parameter: " + param) } else { return defaultValue } } return p } func getConnectionString() string { host := getParamString("db.host", "localhost") port := getParamString("db.port", "3306") user := getParamString("db.user", "hoge") pass := getParamString("db.password", "fuga") dbname := getParamString("db.name", "test") protocol := getParamString("db.protocol", "tcp") dbargs := getParamString("dbargs", " ") timezone := getParamString("db.timezone", "parseTime=true&loc=Asia%2FTokyo") if strings.Trim(dbargs, " ") != ""{ dbargs = "?" + dbargs } else { dbargs = "" } return fmt.Sprintf("%s:%s@%s([%s]:%s)/%s%s?%s", user, pass, protocol, host, port, dbname, dbargs, timezone) }
app/init.go
func init() { revel.OnAppStart(models.InitDB) }
なんかエラーになりますね。。