import ( _ "github.com/go-sql-driver/mysql" "github.com/go-xorm/xorm" ) func main() { engine, err := xorm.NewEngine("mysql", "user:password@tcp(localhost:3306)/db_name") if err != nil { panic(err) } // use the engine variable to access the database }
type User struct { Id int `xorm:"int(11) autoincr pk"` Name string `xorm:"varchar(25)"` Age int `xorm:"int(11)"` Email string `xorm:"varchar(25)"` } func main() { engine.CreateTables(&User{}) }
func main() { user := &User{Name: "John", Age: 25, Email: "[email protected]"} _, err := engine.Insert(user) if err != nil { panic(err) } }In this example, we are inserting a new User object into the database using the Insert method of the engine variable. Overall, the go-xorm package is a powerful tool for data access in Golang applications, and its easy-to-use API makes database operations more efficient and streamlined.