-6

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.

JimB
  • 104,193
  • 13
  • 262
  • 255
shadow
  • 99
  • 6
  • 2
    Please edit your question to include the relevant code *in the body of the question*, not at an external link. – Adrian Oct 04 '21 at 14:44

2 Answers2

1

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

emptyhua
  • 6,634
  • 10
  • 11
0

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)

梁先森
  • 1
  • 1