Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a uint64 into uint?

Tags:

go

I have the following function:

func (rc ResizeController) Resize(c *gin.Context) {

    height := c.Query("height")
    width := c.Query("width")
    filepath := c.Query("file")

    h, err := strconv.ParseUint(height, 10, 32)
    w, err := strconv.ParseUint(width, 10, 32)

    file, err := os.Open("./test_images/" + filepath)

    if err != nil {
        log.Fatal(err)
    }

    image, err := jpeg.Decode(file)

    if err != nil {
        log.Fatal(err)
    }

    m := resize.Resize(1000, 100, image, resize.Lanczos3)

    buf := new(bytes.Buffer)
    jpeg.Encode(buf, m, nil)
    response := buf.Bytes()

    c.Data(200, "image/jpeg", response)
}

But I get the following error:

controllers/resize_controller.go:41: cannot use h (type uint64) as type uint in argument to resize.Resize
controllers/resize_controller.go:41: cannot use w (type uint64) as type uint in argument to resize.Resize

I've tried a few different functions from the strconv lib with no luck!

like image 764
Ewan Valentine Avatar asked Sep 02 '15 20:09

Ewan Valentine


1 Answers

No need to use any of the strconv functions; just do a type conversion to uint:

h64, err := strconv.ParseUint(height, 10, 32)
if err != nil {
    // TODO: handle error
}
w64, err := strconv.ParseUint(width, 10, 32)
if err != nil {
    // TODO: handle error
}
h := uint(h64)
w := uint(w64)
like image 141
Tim Cooper Avatar answered Nov 15 '22 03:11

Tim Cooper