本文整理匯總了Golang中database/sql.NullString.Scan方法的典型用法代碼示例。如果您正苦於以下問題:Golang NullString.Scan方法的具體用法?Golang NullString.Scan怎麽用?Golang NullString.Scan使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類database/sql.NullString
的用法示例。
在下文中一共展示了NullString.Scan方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Scan
// Scan implements the Scanner interface.
func (ns *ByteSlice) Scan(value interface{}) error {
n := sql.NullString{String: base64.StdEncoding.EncodeToString(ns.ByteSlice)}
err := n.Scan(value)
//ns.Float32, ns.Valid = float32(n.Float64), n.Valid
ns.ByteSlice, err = base64.StdEncoding.DecodeString(n.String)
ns.Valid = n.Valid
return err
}
示例2: Scan
func (this *ERole) Scan(value interface{}) error {
var x sql.NullString
err := x.Scan(value)
if !x.Valid || err != nil {
return err
}
*this = ERole(x.String)
return nil
}
示例3: Scan
func (this *Str) Scan(value interface{}) error {
var s sql.NullString
err := s.Scan(value)
if !s.Valid || err != nil {
return err
}
*this = Str(s.String)
return nil
}
示例4: main
func main() {
db, err := sqlx.Connect("mysql", "root:[email protected](localhost:3306)/bookszilla?parseTime=true")
if err != nil {
log.Fatalln(err)
}
db.Ping()
for i := 1; i <= 1000; i++ {
url := "http://wmate.ru/ebooks/book" + strconv.Itoa(i) + ".html"
response, err := http.Get(url)
println(url + " " + response.Status)
if err != nil {
fmt.Printf("%s", err)
// os.Exit(1)
} else if response.StatusCode == 200 {
defer response.Body.Close()
doc, err := goquery.NewDocumentFromResponse(response)
if err != nil {
log.Fatal(err)
}
doc.Find("article.post").Each(func(i int, s *goquery.Selection) {
title := s.Find("h1").Text()
println(title)
// author := s.Find("ul.info li").Eq(2).Find("em").Text()
// println(author)
var desctiption sql.NullString
desc := ""
s.Find("p").Each(func(j int, sp *goquery.Selection) {
desc += strings.TrimSpace(sp.Text()) + "\n"
})
if len(desc) > 0 {
desctiption.Scan(desc)
}
// println(desctiption)
sql := "INSERT INTO `books` (`name`, `description`) VALUES (?, ?);"
db.Exec(sql, title, desctiption)
})
}
}
}
示例5: Login
// Input: email string, password string
// Output:
// - Success: session string
// - Failed: {}
func Login(w http.ResponseWriter, r *http.Request) {
var email sql.NullString
email.Scan(r.FormValue("email"))
password := r.FormValue("password")
user := models.User{Email: email, Password: password}
if !repository.Login(&user) {
w.WriteHeader(http.StatusForbidden)
w.Write([]byte(`{"error":"Mat khau hoac email khong dung"}`))
return
}
createUserToken(&user)
repository.UpdateUser(&user)
json.NewEncoder(w).Encode(user)
}
示例6: TestNullTypeString
func TestNullTypeString(t *testing.T) {
var b Sqlizer
var name sql.NullString
b = Eq{"name": name}
sql, args, err := b.ToSql()
assert.NoError(t, err)
assert.Empty(t, args)
assert.Equal(t, "name IS NULL", sql)
name.Scan("Name")
b = Eq{"name": name}
sql, args, err = b.ToSql()
assert.NoError(t, err)
assert.Equal(t, []interface{}{"Name"}, args)
assert.Equal(t, "name = ?", sql)
}
示例7: Scan
func (s *scanner) Scan(src interface{}) error {
var err error
switch s.value.Type().Kind() {
case reflect.Struct:
nt := mysql.NullTime{}
err := nt.Scan(src)
if err != nil {
return err
}
s.value.Set(reflect.ValueOf(nt.Time))
case reflect.Bool:
nb := sql.NullBool{}
err := nb.Scan(src)
if err != nil {
return err
}
s.value.SetBool(nb.Bool)
case reflect.String:
ns := sql.NullString{}
err = ns.Scan(src)
if err != nil {
return err
}
s.value.SetString(ns.String)
case reflect.Int64:
ni := sql.NullInt64{}
err = ni.Scan(src)
if err != nil {
return err
}
s.value.SetInt(ni.Int64)
case reflect.Float64:
ni := sql.NullFloat64{}
err = ni.Scan(src)
if err != nil {
return err
}
s.value.SetFloat(ni.Float64)
}
return nil
}
示例8: fetchResult
func fetchResult(itemT reflect.Type, rows *sql.Rows, columns []string) (reflect.Value, error) {
var item reflect.Value
var err error
switch itemT.Kind() {
case reflect.Map:
item = reflect.MakeMap(itemT)
case reflect.Struct:
item = reflect.New(itemT)
default:
return item, db.ErrExpectingMapOrStruct
}
expecting := len(columns)
// Allocating results.
values := make([]*sql.RawBytes, expecting)
scanArgs := make([]interface{}, expecting)
for i := range columns {
scanArgs[i] = &values[i]
}
if err = rows.Scan(scanArgs...); err != nil {
return item, err
}
// Range over row values.
for i, value := range values {
if value != nil {
// Real column name
column := columns[i]
// Value as string.
svalue := string(*value)
var cv reflect.Value
v, _ := to.Convert(svalue, reflect.String)
cv = reflect.ValueOf(v)
switch itemT.Kind() {
// Destination is a map.
case reflect.Map:
if cv.Type() != itemT.Elem() {
if itemT.Elem().Kind() == reflect.Interface {
cv, _ = util.StringToType(svalue, cv.Type())
} else {
cv, _ = util.StringToType(svalue, itemT.Elem())
}
}
if cv.IsValid() {
item.SetMapIndex(reflect.ValueOf(column), cv)
}
// Destionation is a struct.
case reflect.Struct:
index := util.GetStructFieldIndex(itemT, column)
if index == nil {
continue
} else {
// Destination field.
destf := item.Elem().FieldByIndex(index)
if destf.IsValid() {
if cv.Type() != destf.Type() {
if destf.Type().Kind() != reflect.Interface {
switch destf.Type() {
case nullFloat64Type:
nullFloat64 := sql.NullFloat64{}
if svalue != `` {
nullFloat64.Scan(svalue)
}
cv = reflect.ValueOf(nullFloat64)
case nullInt64Type:
nullInt64 := sql.NullInt64{}
if svalue != `` {
nullInt64.Scan(svalue)
}
cv = reflect.ValueOf(nullInt64)
case nullBoolType:
nullBool := sql.NullBool{}
if svalue != `` {
nullBool.Scan(svalue)
}
cv = reflect.ValueOf(nullBool)
case nullStringType:
nullString := sql.NullString{}
nullString.Scan(svalue)
cv = reflect.ValueOf(nullString)
default:
var decodingNull bool
if svalue == "" {
//.........這裏部分代碼省略.........
示例9:
// "fmt"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/triitvn/instagram-go/api/models/repository"
"net/url"
"strconv"
)
var _ = Describe("User", func() {
var params = url.Values{}
params.Add("displayName", "Foo")
params.Add("email", "[email protected]")
params.Add("password", "123456")
var email sql.NullString
email.Scan("[email protected]")
repository.DeleteUserByEmail(email)
var userId string
Describe("Register", func() {
Context("Register Success", func() {
It("should have ID", func() {
response := Request("POST", "/user?"+params.Encode(), "")
objmap := JsonStrToMap(response.Body)
userId = strconv.Itoa(int(objmap["Id"].(float64)))
示例10: Scan
// Scan implements the Scanner interface.
func (ns *String) Scan(value interface{}) error {
n := sql.NullString{String: ns.String}
err := n.Scan(value)
ns.String, ns.Valid = n.String, n.Valid
return err
}