Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Convert string to uint64 in Go

In Go language, you can convert a string to an unsigned 64-bit integer (uint64) using the strconv package's ParseUint function. Here's an example:

package main

import (
    "fmt"
    "strconv"
)

func main() {
    str := "123456789"
    i, err := strconv.ParseUint(str, 10, 64)
    if err != nil {
        fmt.Println("Error:", err)
        return
    }
    fmt.Println(i)
}


In this example, we first define a string str containing the number we want to convert. We then call strconv.ParseUint function, which takes three arguments:

The first argument is the string we want to convert (str).
The second argument is the base of the integer representation in the string (10 for decimal).
The third argument is the bit size of the resulting integer (64 for a uint64).
The function returns two values: the converted integer (i) and an error (err). We check if the error is nil (indicating success) and print the converted integer to the console.