Home > database >  assert interface{} to int64
assert interface{} to int64

Time:10-05

i am using gin, i use c.shouldBind to bind the data to the struct, the use c.set to set the params in the context. when i use c.getInt64 to get the params, it can't return the value i set in the context(i set a 1), it return a zero. it failed to assert a float64 1 to a int64 0.

i have google it but can't get the answer i want

here are my debug code

// GetInt64 returns the value associated with the key as an integer.
func (c *Context) GetInt64(key string) (i64 int64) {
    if val, ok := c.Get(key); ok && val != nil {
        i64, _ = val.(int64)
    }
    return
}

the val is 1, but it returns 0

So can anybody tell me why and how to solve it.

CodePudding user response:

Golang can't convert types implicitly. You can use the GetFloat64 method.

CodePudding user response:

It depends the value which you really set in, and check the key is right, After that, Maybe you could use assertion after Get like: value := c.Get("From_validator_page").(Int64)

  • Related