Convert string to uint in go lang

I am trying to convert a string in uint to 32-bit ubuntu using the following code. But it always converts it to uint64, despite explicitly passing 32 as an argument to a function. Below in the mw code is the object image of the magic library. Returns uintwhen mw.getImageWidth()and are called mw.getImageHeight(). In addition, it takes an argument of uinttype resize .

    width :=  strings.Split(imgResize, "x")[0]
    height := strings.Split(imgResize, "x")[1]

    var masterWidth uint = mw.GetImageWidth() 
    var masterHeight uint = mw.GetImageHeight() 

    mw := imagick.NewMagickWand()
    defer mw.Destroy()

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

    var masterWidth uint = mw.GetImageWidth() 
    var masterHeight uint = mw.GetImageHeight()

    wd, _ := strconv.ParseUint(width, 10, 32)
    ht, _ := strconv.ParseUint(height, 10, 32)

   if masterWidth < wd || masterHeight < ht { 
     err = mw.ResizeImage(wd, ht, imagick.FILTER_BOX, 1)
     if err != nil {
        panic(err)
    } 
   }

Mistake:

# command-line-arguments
test.go:94: invalid operation: masterWidth < wd (mismatched types uint and uint64)
goImageCode/test.go:94: invalid operation: masterHeight < ht (mismatched types uint and uint64)
goImageCode/test.go:100: cannot use wd (type uint64) as type uint in argument to mw.ResizeImage
goImageCode/AmazonAWS.go:100: cannot use ht (type uint64) as type uint in argument to mw.ResizeImage
+4
source share
1 answer

Strconv package

func ParseUint

func ParseUint(s string, base int, bitSize int) (n uint64, err error)

ParseUint is similar to ParseInt, but for unsigned numbers.

func parseInt

func ParseInt(s string, base int, bitSize int) (i int64, err error)

ParseInt s ( 2 36) i. base == 0, : 16 "0x", 8 "0" 10 .

bitSize , . 0, 8, 16, 32 64 int, int8, int16, int32 int64.

, ParseInt, * NumError include err.Num = s. s , err.Err = ErrSyntax, 0; , s, , err.Err = ErrRange, .

bitSize , . uint , 32, 64 . ParseUint uint64. ,

package main

import (
    "fmt"
    "strconv"
)

func main() {
    width := "42"
    u64, err := strconv.ParseUint(width, 10, 32)
    if err != nil {
        fmt.Println(err)
    }
    wd := uint(u64)
    fmt.Println(wd)
}

:

42
+8

Source: https://habr.com/ru/post/1627028/


All Articles