import ( "database/sql" "github.com/go-gorp/gorp" _ "github.com/mattn/go-sqlite3" ) func main() { db, err := sql.Open("sqlite3", "./test.db") if err != nil { log.Fatal(err) } dbmap := &gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}} }
type Person struct { Id int64 Name string Age int } func main() { [...] dbmap.AddTableWithName(Person{}, "persons").SetKeys(true, "Id") p1 := &Person{Name: "John Doe", Age: 30} err = dbmap.Insert(p1) if err != nil { log.Fatal(err) } }In this example, we are first defining a Go struct representing a table called "persons". We then use the AddTableWithName method of the DbMap object to map the Go struct to the database table. Finally, we insert a new record into the table using the Insert method of the DbMap object.