gdbinder is light-weight data mapper for Golang.
Usally, I know and love gorp. gorp is very powerful and useful. But if many column defined table mapped (for example over 100 columns), It's too slow to use my projects.
gdbinder is generate static data mapping Go code. So that it made it possible to reduce using of reflection.
go get -u github.com/Jun-Chang/gdbinder
Prepare code as follows.
package example
import "database/sql"
//go:generate gdbinder -type=TestStruct
type TestStruct struct {
ID int `db:"id" foo:"var"`
Col1 int `db:"col1"`
Col2 int `db:"col2"`
Col3 int `db:"col3"`
Col4 int `db:"col4"`
Col5 int `db:"col5"`
Col6 int `db:"col6"`
Col7 int `db:"col7"`
Col8 int `db:"col8"`
Col9 int `db:"col9"`
}
Run go generate.
cd your_code_dir
go generate
Data mapper code will be generated.
// DO NOT EDIT!
// Code generated by gdbinder <https://github.com/Jun-Chang/gdbinder>
package example
import "database/sql"
func TestStructClmns() string {
return "id,col1,col2,col3,col4,col5,col6,col7,col8,col9"
}
func TestStructScan(rs *sql.Rows) ([]*TestStruct, error) {
bound := []*TestStruct{}
for rs.Next() {
b := TestStruct{}
if err := rs.Scan(&b.ID, &b.Col1, &b.Col2, &b.Col3, &b.Col4, &b.Col5, &b.Col6, &b.Col7, &b.Col8, &b.Col9); err != nil {
return nil, err
}
bound = append(bound, &b)
}
return bound, nil
}
You can use to generated code in your own code.
func FindStruct(db *sql.DB) ([]*TestStruct, error) {
query := "SELECT * FROM gdbinder_test"
rs, err := db.Query(query)
if err != nil {
return nil, err
}
defer rs.Close()
return TestStructScan(rs)
}
BenchmarkGdbinderColumn10-5 20000 68664 ns/op 1624 B/op 39 allocs/op
BenchmarkGorpColumn10-5 10000 108145 ns/op 6363 B/op 198 allocs/op
BenchmarkGdbinderColumn100-5 10000 146970 ns/op 14888 B/op 309 allocs/op
BenchmarkGorpColumn100-5 200 8939167 ns/op 205025 B/op 10818 allocs/op
gdbinder is licensed under the MIT