Golang中国
package main

import (
    //"crypto"
    "fmt"
    "time"

    _ "github.com/go-sql-driver/mysql"
    "github.com/lunny/xorm"
)

var (
    Engine *xorm.Engine
)

type User struct {
    Id            int64
    Email         string `xorm:"index"`
    Password      string
    Nickname      string `xorm:"index"`
    Realname      string
    Avatar        string
    Avatar_min    string
    Avatar_max    string
    Birth         time.Time
    Province      string
    City          string
    Company       string
    Address       string
    Postcode      string
    Mobile        string
    Website       string
    Sex           int64
    Qq            string
    Msn           string
    Weibo         string
    Ctype         int64
    Role          int64
    Created       time.Time `xorm:"index"`
    Hotness       float64   `xorm:"index"`
    Hotup         int64     `xorm:"index"`
    Hotdown       int64     `xorm:"index"`
    Views         int64     `xorm:"index"`
    LastLoginTime time.Time
    LastLoginIp   string
    LoginCount    int64
}

func main() {
    Engine, err := xorm.NewEngine("mysql", "root:1111@/webtest?charset=utf8")
    if err == nil {
        fmt.Println("Engine is OK")
    }
    if err := Engine.Sync(new(User)); err != nil {
        fmt.Println("Database struct sync failed")
        fmt.Println("Engine.Sync ERRORS:", err)
    } else {
        fmt.Println("Database struct sync successfully")
    }

    adduserErr := AddUser("root@localhost", "root", "系统默认管理员", "111", -1000)

    if adduserErr != nil {
        fmt.Println("...")
    }

    defer Engine.Close()
}

func AddUser(email string, nickname string, realname string, password string, role int64) error {
    _, err := Engine.Insert(&User{Email: email, Nickname: nickname, Realname: realname, Password: password, Role: role, Created: time.Now()})

    return err
}

我刚接触xorm 版本:golang:1.2.1 mysql:5.6 异常:

Engine is OK
    Database struct sync successfully

    panic: runtime error: invalid memory address or nil pointer dereference
    [signal 0xc0000005 code=0x0 addr=0xc0 pc=0x4a4df8]

    goroutine 1 [running]:
    runtime.panic(0x66f180, 0x974a6f)
        C:/Users/ADMINI~1/AppData/Local/Temp/2/makerelease250988475/go/src/pkg/runtime/panic.c:266 +0xc8
    github.com/lunny/xorm.(*Statement).Init(0xc084060e18)
        E:/GoSrc/src/github.com/lunny/xorm/statement.go:69 +0x228
    github.com/lunny/xorm.(*Session).Init(0xc084060e00)
        E:/GoSrc/src/github.com/lunny/xorm/session.go:39 +0x84
    github.com/lunny/xorm.(*Engine).NewSession(0x0, 0x328000)
        E:/GoSrc/src/github.com/lunny/xorm/engine.go:170 +0x5b
    github.com/lunny/xorm.(*Engine).Insert(0x0, 0x372df0, 0x1, 0x1, 0x0, ...)
        E:/GoSrc/src/github.com/lunny/xorm/engine.go:922 +0x4a
    main.AddUser(0x6d1e70, 0xe, 0x6bec70, 0x4, 0x6ec3b0, ...)
        E:/GoSrc/src/gotest.go:71 +0x171
    main.main()
        E:/GoSrc/src/gotest.go:61 +0x3f5

    goroutine 3 [chan receive]:
    database/sql.(*DB).connectionOpener(0xc084041180)
        C:/Users/ADMINI~1/AppData/Local/Temp/2/makerelease250988475/go/src/pkg/database/sql/sql.go:574 +0x41
    created by database/sql.Open
        C:/Users/ADMINI~1/AppData/Local/Temp/2/makerelease250988475/go/src/pkg/database/sql/sql.go:436 +0x250

    goroutine 10 [runnable]:
    database/sql.(*DB).connectionOpener(0xc08405bc00)
        C:/Users/ADMINI~1/AppData/Local/Temp/2/makerelease250988475/go/src/pkg/database/sql/sql.go:574 +0x41
    created by database/sql.Open
        C:/Users/ADMINI~1/AppData/Local/Temp/2/makerelease250988475/go/src/pkg/database/sql/sql.go:436 +0x250
    exit status 2

    exit status 1
    enter code here
4 回复
jimmykuu
#1 jimmykuu • 2014-04-07 00:26

50行的Engine是局部变量,全局的Engine并没有初始化。

hongkangzy
#2 hongkangzy • 2016-01-28 16:16

xorm的异常处理文档在哪?

xiaolunwen
#3 xiaolunwen • 2016-01-29 00:05

这个错误是Go的一个坑,其实和xorm关系不大。

wahhung
#4 wahhung • 2016-12-01 01:29

50 行 Engine, err := xorm.NewEngine(“mysql”, “root:1111@/webtest?charset=utf8”)。其中 用到 “:=”,即初始化了一个局部变量Engine,和13行的 Engine *xorm.Engine 不是同一个 变量。将50行改成 Engine, err = xorm.NewEngine(“mysql”, “root:1111@/webtest?charset=utf8”) 即可

需要 登录 后方可回复, 如果你还没有账号你可以 注册 一个帐号。