同步操作将从 DiDi-opensource/gendry 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
import (
"database/sql"
"fmt"
"github.com/didi/gendry/scanner"
)
type Person struct {
Name string `ddb:"name"`
Age int `ddb:"m_age"`
}
rows,_ := db.Query("select name,m_age from person")
var students []Person
err := scanner.Scan(rows, &students)
for _,student := range students {
fmt.Println(student)
}
Make sure the second param of Scan should be a reference
ScanClose
is the same as the Scan but it also close the rows so you dont't need to worry about closing the rows yourself.
ScanMap returns the result in the form of []map[string]interface{}, sometimes this could be more convenient.
rows,_ := db.Query("select name,m_age from person")
result,err := scanner.ScanMap(rows)
for _,record := range result {
fmt.Println(record["name"], record["m_age"])
}
If you don't want to define a struct,ScanMap may be useful.But the returned the map is map[string]interface{}
, and interface{}
is pretty unclear like the void *
in C
or Object
in JAVA
, it'll suck you sooner or later.
ScanMapClose is the same as ScanMap but it also close the rows
Map
convert a struct into a map which could easily be used to insert
Test cases blow may make sense
package scaner
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestMap(t *testing.T) {
type Person struct {
Name string `ddb:"name"`
Age int `ddb:"age"`
foo byte `ddb:"foo"`
}
a := Person{"deen", 22, 1}
b := &Person{"caibirdme", 23, 1}
c := &b
mapA, err := Map(a, DefaultTagName)
ass := assert.New(t)
ass.NoError(err)
ass.Equal("deen", mapA["name"])
ass.Equal(22, mapA["age"])
_, ok := mapA["foo"]
ass.False(ok)
mapB, err := Map(c, "")
ass.NoError(err)
ass.Equal("caibirdme", mapB["Name"])
ass.Equal(23, mapB["Age"])
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。