package main import ( "github.com/gin-gonic/gin" "github.com/go-admin-team/go-admin/v3" "github.com/go-admin-team/go-admin/v3/modules/config" "github.com/go-admin-team/go-admin/v3/plugins/admin" _ "github.com/go-admin-team/go-admin/v3/plugins/example" ) func main() { r := gin.Default() // initialize Admin module engine := admin.NewAdmin() // add urls and middleware to engine engine.AddHandler(r) engine.Use(r) // setup Admin configuration cfg := config.Config{ Databases: config.DatabaseList{ "default": { Host: "localhost", Port: "3306", User: "root", Password: "123456", Name: "test", }, }, UrlPrefix: "admin", } engine.Init(cfg) // run server r.Run(":8080") }
package main import ( "github.com/gin-gonic/gin" "github.com/go-admin-team/go-admin/v3" "github.com/go-admin-team/go-admin/v3/modules/config" "github.com/go-admin-team/go-admin/v3/plugins/admin" _ "github.com/go-admin-team/go-admin/v3/plugins/example" ) func main() { r := gin.Default() // initialize Admin module engine := admin.NewAdmin() // add urls and middleware to engine engine.AddHandler(r) engine.Use(r) // setup Admin configuration cfg := config.Config{ Databases: config.DatabaseList{ "default": { Host: "localhost", Port: "3306", User: "root", Password: "123456", Name: "test", }, }, UrlPrefix: "admin", } engine.Init(cfg) // add a new user table userTable := admin.NewDefaultTable(admin.DefaultTableName("users")) userTable = userTable.SetDeleteFn(func(model pk.IModel) error { return db().Table("users").Where("id", "=", model.(UserModel).Id).Delete(UserModel{}).Error }).SetEditFn(func(model pk.IModel) interface{} { return &UserModelForm{Id: model.(UserModel).Id, Username: model.(UserModel).Username} }).SetInsertFn(func(values form.Values) error { return db().Table("users").Create(UserModel{Username: values.Get("username")}).Error }).SetUpdateFn(func(values form.Values) error { return db().Table("users").Where("id", "=", values.Get("id")).Update(UserModel{Username: values.Get("username")}).Error }) // register table with engine engine.AddTable(userTable) // run server r.Run(":8080") } // define UserModel object and accompanying form type UserModel struct { gorm.Model Username string `json:"username" gorm:"unique"` } type UserModelForm struct { Id uint `json:"id" validate:"required"` Username string `json:"username" validate:"required"` } // define helper function to access database instance func db() *gorm.DB { return gorm.DB.DB("default").Model(&UserModel{}) }In this example, we first initialize the Admin module and set up the database connection. We then define a new table for managing user data, and register it with the engine. Finally, we define helper functions for accessing the database instance and starting the server. This allows us to easily manage user data through a web interface. Package Library: go-admin-team/go-admin/v3