我如何在golang中将uint64转换为uint?

时间:2015-09-02 20:35:54

标签: go

我有以下功能:

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)
}

但是我收到以下错误:

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

我从strconv lib尝试了一些不同的功能而没有运气!

提前致谢

1 个答案:

答案 0 :(得分:17)

无需使用任何strconv函数;只需type conversionuint

h64, err := strconv.ParseUint(height, 10, 32)
// TODO: check err
w64, err := strconv.ParseUint(width, 10, 32)
// TODO: check err
h := uint(h64)
w := uint(w64)