Golang database/sql.Rows.Scan function examples
package database/sql
Scan copies the columns in the current row into the values pointed at by dest.
If an argument has type *[]byte, Scan saves in that argument a copy of the corresponding data. The copy is owned by the caller and can be modified and held indefinitely. The copy can be avoided by using an argument of type *RawBytes instead; see the documentation for RawBytes for restrictions on its use.
If an argument has type *interface{}, Scan copies the value provided by the underlying driver without conversion. If the value is of type []byte, a copy is made and the caller owns the result.
Golang database/sql.Rows.Scan function usage examples
Example 1:
var out bool
rows = dbt.mustQuery("SELECT value FROM test")
if rows.Next() {
rows.Scan(&out) //<-- here
if true != out {
dbt.Errorf("true != %t", out)
}
if rows.Next() {
dbt.Error("unexpected data")
}
} else {
dbt.Error("no data")
}
Example 2:
var target interface{}
if rows.Next() {
err := rows.Scan(target)
return err
}
Example 3:
rows, err := db.conn.Query("SELECT entity_id, name FROM edge where parent_id = ?;", e.id)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var entityId, entityName string
if err := rows.Scan(&entityId, &entityName); err != nil { // <--- here
return nil, err
}
...
References :
https://github.com/go-sql-driver/mysql/blob/master/driver_test.go
Advertisement
Something interesting
Tutorials
+6.5k Golang : Combine slices of complex numbers and operation example
+11.8k Golang : Verify Linux user password again before executing a program example
+13.4k Golang : Increment string example
+5.1k Golang : Check if a word is countable or not
+21.6k Golang : GORM create record or insert new record into database example
+8.4k Golang : Generate Datamatrix barcode
+7.9k Golang : Get today's weekday name and calculate target day distance example
+8.2k Golang : Get final or effective URL with Request.URL example
+24.6k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?
+7.9k Swift : Convert (cast) String to Float
+5.3k Golang : Get FX sentiment from website example
+6.6k Golang : Totalize or add-up an array or slice example