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

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

示例1: TestPositiveFloatOrNull

func TestPositiveFloatOrNull(t *testing.T) {
    var (
        nullFloat sql.NullFloat64
        value     driver.Value
        err       error
    )

    // When the number is negative
    nullFloat = PositiveFloatOrNull(-0.5)

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

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

    // When the number is greater than zero
    nullFloat = PositiveFloatOrNull(1.5)

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

    // nullFloat.Value() should return the integer
    value, err = nullFloat.Value()
    assert.Nil(t, err)
    assert.Equal(t, 1.5, value)
}

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

示例2: checkFloatForNull

func checkFloatForNull(eventFloat string, event *sql.NullFloat64) {
    var err error

    if eventFloat == "" {
        event.Valid = false
    } else {
        event.Float64, err = strconv.ParseFloat(eventFloat, 64)
        if err != nil {
            event.Valid = false
            return
        }

        event.Valid = true
    }
}

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

示例3: Scan

//Scan a value into the float64, error on nil or unparseable
func (f *Float64) Scan(value interface{}) error {
    tmp := sql.NullFloat64{}
    tmp.Scan(value)

    if tmp.Valid == false {
        return errors.New("Value should be a float64 and not nil")
    }

    f.Float64 = tmp.Float64

    f.DoInit(func() {
        f.shadow = tmp.Float64
    })

    return nil
}

开发者ID:c2h5oh,项目名称:norm,代码行数:17,代码来源:float64.go

示例4: AddActivity

func (db *Database) AddActivity(type_id int64, description string, user_id int64, is_public bool, lat, long string) error {
    var latitude sql.NullFloat64
    var longitude sql.NullFloat64

    var err error
    if latitude.Float64, err = strconv.ParseFloat(lat, 64); err != nil {
        latitude.Valid = false
    } else {
        latitude.Valid = true
    }

    if longitude.Float64, err = strconv.ParseFloat(long, 64); err != nil {
        longitude.Valid = false
    } else {
        longitude.Valid = true
    }

    public := 0
    if is_public {
        public = 1
    }

    txn, err := db.conn.Begin()
    if err != nil {
        log.Printf("Starting transaction failed: %v", err)
        return err
    }

    row := txn.QueryRow("SELECT count(1) FROM activity_type_flags WHERE type_id = ? AND flag_id = ?", type_id, db.flagNameToIdMapping[FLAG_POINT_IN_TIME])
    var point_in_time_count int64
    if err = row.Scan(&point_in_time_count); err != nil {
        log.Printf("row.Scan failed: %v", err)
        txn.Rollback()
        return err
    }

    result, err := txn.Exec("INSERT INTO activities (type_id, timestamp, description, user_id, public, latitude, longitude) VALUES (?, NOW(), ?, ?, ?, ?, ?)", type_id, description, user_id, public, latitude, longitude)
    if err == nil {
        activity_id, _ := result.LastInsertId()
        if point_in_time_count != 0 {
            _, err = txn.Exec("UPDATE activities SET end_timestamp = NOW() WHERE id = ?", activity_id)
            if err != nil {
                log.Printf("db.conn.Exec failed: %v", err)
            }
        }
    }

    if err != nil {
        txn.Rollback()
    } else {
        err = txn.Commit()
        if err != nil {
            log.Printf("committing transaction failed: %v", err)
        }
    }
    return err
}

开发者ID:bignoyrb,项目名称:activitylog,代码行数:57,代码来源:db.go

示例5: AddActivity

func AddActivity(w http.ResponseWriter, r *http.Request) {
    session, _ := store.Get(r, SESSION_NAME)
    if authenticated, ok := session.Values["Authenticated"].(bool); !ok || !authenticated {
        http.Error(w, "unauthenticated", http.StatusForbidden)
        return
    }

    username := session.Values["UserName"].(string)
    user_id := session.Values["UserId"].(int64)

    type_id := r.FormValue("type_id")
    description := r.FormValue("desc")
    is_public := 0
    if r.FormValue("public") == "on" {
        is_public = 1
    }

    var err error
    var latitude sql.NullFloat64
    var longitude sql.NullFloat64

    if latitude.Float64, err = strconv.ParseFloat(r.FormValue("lat"), 64); err != nil {
        latitude.Valid = false
    } else {
        latitude.Valid = true
    }

    if longitude.Float64, err = strconv.ParseFloat(r.FormValue("long"), 64); err != nil {
        longitude.Valid = false
    } else {
        longitude.Valid = true
    }

    if _, err := db.Exec("INSERT INTO activities (type_id, timestamp, description, user_id, public, latitude, longitude) VALUES (?, NOW(), ?, ?, ?, ?, ?)", type_id, description, user_id, is_public, latitude, longitude); err != nil {
        log.Printf("AddActivity: db.Exec failed: %v", err)
    } else {
        log.Printf("added activity %s (type_id = %s) for user %s", description, type_id, username)
    }

    fmt.Fprintf(w, "OK")
}

开发者ID:Gehry,项目名称:activitylog,代码行数:41,代码来源:activities.go

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