本文整理匯總了Golang中database/sql.NullInt64.Value方法的典型用法代碼示例。如果您正苦於以下問題:Golang NullInt64.Value方法的具體用法?Golang NullInt64.Value怎麽用?Golang NullInt64.Value使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類database/sql.NullInt64
的用法示例。
在下文中一共展示了NullInt64.Value方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestPositiveIntOrNull
func TestPositiveIntOrNull(t *testing.T) {
var (
nullInt sql.NullInt64
value driver.Value
err error
)
// When the number is negative
nullInt = PositiveIntOrNull(-1)
// nullInt.Valid should be false
assert.False(t, nullInt.Valid)
// nullInt.Value() should return nil
value, err = nullInt.Value()
assert.Nil(t, err)
assert.Nil(t, value)
// When the number is greater than zero
nullInt = PositiveIntOrNull(1)
// nullInt.Valid should be true
assert.True(t, nullInt.Valid)
// nullInt.Value() should return the integer
value, err = nullInt.Value()
assert.Nil(t, err)
assert.Equal(t, int64(1), value)
}
示例2: 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
}
示例3: TestIntOrNull
func TestIntOrNull(t *testing.T) {
var nullInt sql.NullInt64
var value driver.Value
var err error
// When the integer is zero
nullInt = IntOrNull(0)
// nullInt.Valid should be false
assert.False(t, nullInt.Valid)
// nullInt.Value() should return nil
value, err = nullInt.Value()
assert.Nil(t, err)
assert.Nil(t, value)
// When the integer is greater than zero
nullInt = IntOrNull(1)
// nullInt.Valid should be true
assert.True(t, nullInt.Valid)
// nullInt.Value() should return the integer
value, err = nullInt.Value()
assert.Nil(t, err)
assert.Equal(t, int64(1), value)
}