本文整理汇总了Golang中database/sql.NullString类的典型用法代码### 示例。如果您正苦于以下问题:Golang NullString类的具体用法?Golang NullString怎么用?Golang NullString使用的例子?那么恭喜您, 这里精选的类代码### 示例或许可以为您提供帮助。

在下文中一共展示了NullString类的20个代码### 示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码### 示例。

示例1: SaveThing

func (w *DatabaseWorld) SaveThing(thing *Thing) (ok bool) {
    tabletext, err := json.Marshal(thing.Table)
    if err != nil {
        log.Println("Error serializing table data for thing", thing.Id, ":", err.Error())
        return false
    }

    var parent sql.NullInt64
    if thing.Parent != 0 {
        parent.Int64 = int64(thing.Parent)
        parent.Valid = true
    }
    var owner sql.NullInt64
    if thing.Owner != 0 && thing.Type.HasOwner() {
        owner.Int64 = int64(thing.Owner)
        owner.Valid = true
    }
    var program sql.NullString
    if thing.Program != nil {
        program.String = thing.Program.Text
        program.Valid = true
    }

    // TODO: save the allow list
    _, err = w.db.Exec("UPDATE thing SET name = $1, parent = $2, owner = $3, adminlist = $4, denylist = $5, tabledata = $6, program = $7 WHERE id = $8",
        thing.Name, parent, owner, thing.AdminList, thing.DenyList,
        types.JsonText(tabletext), program, thing.Id)
    if err != nil {
        log.Println("Error saving a thing", thing.Id, ":", err.Error())
        return false
    }
    return true
}

开发者ID:natmeox,项目名称:mess,代码行数:33,代码来源:world.go

示例2: TestStringOrNull

func TestStringOrNull(t *testing.T) {
    var (
        nullString sql.NullString
        value      driver.Value
        err        error
    )

    // When the string is empty
    nullString = StringOrNull("")

    // nullString.Valid should be false
    assert.False(t, nullString.Valid)

    // nullString.Value() should return nil
    value, err = nullString.Value()
    assert.Nil(t, err)
    assert.Nil(t, value)

    // When the string is not empty
    nullString = StringOrNull("foo")

    // nullString.Valid should be true
    assert.True(t, nullString.Valid)

    // nullString.Value() should return the string
    value, err = nullString.Value()
    assert.Nil(t, err)
    assert.Equal(t, "foo", value)
}

开发者ID:chenhougen,项目名称:go-oauth2-server,代码行数:29,代码来源:util_test.go

示例3: listAllConnections

func (database Database) listAllConnections() (res DbUsersWithConnections) {
    res = make(DbUsersWithConnections)

    rows, err := database.db.Query(`
        -- Left join because we want users without connections as well
        SELECT u1.id, u1.username, u2.id, u2.username FROM 
            user AS u1 LEFT JOIN connection ON u1.id = connection.fromUser
            LEFT JOIN user AS u2 ON u2.id = connection.toUser
            ORDER BY u1.id
            `)
    checkErr(err)
    defer rows.Close()
    for rows.Next() {
        var fromUser User
        var toUsername sql.NullString
        var toId sql.NullInt64
        err := rows.Scan(&fromUser.Id, &fromUser.Username, &toId, &toUsername)
        checkErr(err)

        if toId.Valid {
            // this user has at least one connection, unpack the nullable values
            toIdValue, _ := toId.Value()
            toUsernameValue, _ := toUsername.Value()
            res[fromUser] = append(res[fromUser], User{toIdValue.(int64), toUsernameValue.(string)})
        } else {
            // this user doesn't have any connections
            res[fromUser] = []User{}
        }
    }
    return res
}

开发者ID:rutchkiwi,项目名称:go-rest-api,代码行数:31,代码来源:db.go

示例4: 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
}

开发者ID:ryanzec,项目名称:going,代码行数:9,代码来源:byte_slice.go

示例5: checkStringForNull

func checkStringForNull(eventStr string, event *sql.NullString) {
    if len(eventStr) == 0 {
        event.Valid = false
    } else {
        event.String = eventStr
        event.Valid = true
    }
}

开发者ID:Qlean,项目名称:silvia,代码行数:8,代码来源:postgres.go

最后编辑: kuteng  文档更新时间: 2021-08-23 19:14   作者:kuteng