convert string to uint in go lang

Package strconv

func ParseUint

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

ParseUint is like ParseInt but for unsigned numbers.

func ParseInt

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

ParseInt interprets a string s in the given base (2 to 36) and returns
the corresponding value i. If base == 0, the base is implied by the
string’s prefix: base 16 for “0x”, base 8 for “0”, and base 10
otherwise.

The bitSize argument specifies the integer type that the result must
fit into. Bit sizes 0, 8, 16, 32, and 64 correspond to int, int8,
int16, int32, and int64.

The errors that ParseInt returns have concrete type *NumError and
include err.Num = s. If s is empty or contains invalid digits, err.Err
= ErrSyntax and the returned value is 0; if the value corresponding to s cannot be represented by a signed integer of the given size, err.Err
= ErrRange and the returned value is the maximum magnitude integer of the appropriate bitSize and sign.

The bitSize argument specifies the integer type that the result must
fit into. The uint type size is implementation defined, either 32 or 64 bits. The ParseUint return type is always uint64. For example,

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

Output:

42

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)