The problem of inputting uncertain parameters into go

< H1 > question < / H1 >

when registering models using the beego framework, an error is reported:

panic: <orm.RegisterModel> cannot use non-ptr model struct `.`

if you look at the description, you should pass in the pointer, but I passed the pointer (new)

.

main.go

package db

import (
    "github.com/astaxie/beego/orm"
)

func InitDB(models ...interface{}) {
    orm.RegisterDriver("mysql", orm.DRMySQL)
    // set default database
    orm.RegisterDataBase("default", "mysql", "root:*****@tcp(127.0.0.1:3306)/db_test?charset=utf8", 30)
    // register model
    orm.RegisterModel(models)
    // create table
    orm.RunSyncdb("default", false, true)
}

db/main.go

package db

import (
    "github.com/astaxie/beego/orm"
)

func InitDB(models ...interface{}) {
    orm.RegisterDriver("mysql", orm.DRMySQL)
    // set default database
    orm.RegisterDataBase("default", "mysql", "root:123456@tcp(127.0.0.1:3306)/db_test?charset=utf8", 30)
    // register model
    orm.RegisterModel(models)
    // create table
    orm.RunSyncdb("default", false, true)
}

models/user.go

package models

import "hello/db"

type User struct {
    Uid      int `orm:"pk"`
    Username string
    Password string
}
The InitDB function in

db/main.go registers models, and then reports an error

beego document

clipboard.png

should be a grammatical problem

Mar.09,2021
The parameter of

models .interface {} syntax should be [] interface {}
after it is received, so it should be orm.RegisterModel (models.) when passed to RegisterModel as is. Write

like this
  • Questions about data types in golang?

    as shown in the figure, the this.Ctx.Input.Param ( ": pag ") in the beego framework clearly returns string, but why does the error prompt say int? add: Thank you for answering, because this is often written in js and php, and I don t think ther...

    Apr.05,2021
  • Beego cannot find templatefile

    just getting started, I selected a project from the official case and followed the documentation step by step, but kept prompting can t find templatefile in the path:views admin login.tpl I am using beego version 1.10.1 : ask for advice. I gu...

    May.26,2021
  • Problems hidden in beego field

    { "Username":"", "Password":"" } but I need the ability to log in by the user, so the password submitted by the user cannot be verified. How to solve it? ...

    Mar.04,2022
Menu