Version: v1.7.0 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Mar 10, 2021 License: Apache-2.0 Imports: 8 Imported by: 27



import (

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 {

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 (

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.Equal("deen", mapA["name"])
	ass.Equal(22, mapA["age"])
	_, ok := mapA["foo"]
	mapB, err := Map(c, "")
	ass.Equal("caibirdme", mapB["Name"])
	ass.Equal(23, mapB["Age"])
  • Unexported fields will be ignored
  • Ptr type will be ignored
  • Resolve pointer automatically
  • The second param specify what tagName you used in defining your struct.If passed an empty string, FieldName will be returned as the key of the map




View Source
const (
	// DefaultTagName is the default struct tag name
	DefaultTagName = "ddb"


View Source
var (

	//ErrTargetNotSettable means the second param of Bind is not settable
	ErrTargetNotSettable = errors.New("[scanner]: target is not settable! a pointer is required")
	//ErrNilRows means the first param can't be a nil
	ErrNilRows = errors.New("[scanner]: rows can't be nil")
	//ErrSliceToString means only []uint8 can be transmuted into string
	ErrSliceToString = errors.New("[scanner]: can't transmute a non-uint8 slice to string")
	//ErrEmptyResult occurs when target of Scan isn't slice and the result of the query is empty
	ErrEmptyResult = errors.New(`[scanner]: empty result`)
View Source
var (
	// ErrNoneStructTarget as its name says
	ErrNoneStructTarget = errors.New("[scanner] target must be a struct type")


func Map

func Map(target interface{}, useTag string) (map[string]interface{}, error)

Map converts a struct to a map type for each field of the struct must be built-in type

func Scan

func Scan(rows Rows, target interface{}) error

Scan scans data from rows to target Don't forget to close the rows When the target is not a pointer of slice, ErrEmptyResult may be returned if the query result is empty

func ScanClose

func ScanClose(rows Rows, target interface{}) error

ScanClose is the same as Scan and helps you Close the rows Not necessary exec the rows.Close after calling this. Close is idempotent and does not affect the result of Err.

func ScanMap

func ScanMap(rows Rows) ([]map[string]interface{}, error)

ScanMap returns the result in the form of []map[string]interface{} json.Marshal encodes []byte as a base64 string, while in most cases it's expected to be encoded as string or int. If you want this, use ScanMapDecode instead.

func ScanMapClose

func ScanMapClose(rows Rows) ([]map[string]interface{}, error)

ScanMapClose is the same as ScanMap and close the rows

func ScanMapDecode

func ScanMapDecode(rows Rows) ([]map[string]interface{}, error)

ScanMapDecode returns the result in the form of []map[string]interface{} If possible, it will convert []uint8 to int or float64, or it will convert []uint8 to string

func ScanMapDecodeClose

func ScanMapDecodeClose(rows Rows) ([]map[string]interface{}, error)

ScanMapDecodeClose returns the result in the form of []map[string]interface{} If possible, it will convert []uint8 to int or float64, or it will convert []uint8 to string. It will close the rows in the end.

func SetTagName

func SetTagName(name string)

SetTagName can be set only once


type ByteUnmarshaler

type ByteUnmarshaler interface {
	UnmarshalByte(data []byte) error

ByteUnmarshaler is the interface implemented by types that can unmarshal a JSON description of themselves. The input can be assumed to be a valid encoding of a JSON value. UnmarshalByte must copy the JSON data if it wishes to retain the data after returning.

By convention, to approximate the behavior of Unmarshal itself, ByteUnmarshaler implement UnmarshalByte([]byte("null")) as a no-op.

type CloseErr

type CloseErr struct {
	// contains filtered or unexported fields

CloseErr is the error occurs when rows.Close()

func (CloseErr) Error

func (e CloseErr) Error() string

type Rows

type Rows interface {
	Close() error

	Columns() ([]string, error)

	Next() bool

	Scan(dest ...interface{}) error

Rows defines methods that scanner needs, which database/sql.Rows already implements

type ScanErr

type ScanErr struct {
	// contains filtered or unexported fields

ScanErr will be returned if an underlying type couldn't be AssignableTo type of target field

func (ScanErr) Error

func (s ScanErr) Error() string

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL