How to convert a string to an unsigned int in Golang

  • 26 June 2020
  • ADM


How to convert a string to an unsigned int in Golang - images/logos/golang.jpg


Here is a simple snippet how to convert a string into an unsigned int in Golang. If you need help how to install Golang check the references links.


To convert a string to an unsigned int you can use the method strconv.ParseUint. ParseUint interprets a string s in the given base (2 to 36) and returns the corresponding value i.

package main

import (

func main() {

	str := "1234"

	/** converting the str1 variable into an unsigned int using ParseUint method */
	i, err := strconv.ParseUint(str, 10, 64)
	if err == nil {
		fmt.Printf("Type: %T \n", i)


To compile the code navigate to the file location and run the following command.

$ go build string_to_uint.go

Then depending if you are on Linux or Windows the binary file is created.

To run the application execute the command.


$ string_to_uint


c:\Users\adm\go\tutorials> string_to_uint.exe

If you want to compile and run the application in one single step run the following command:

go run string_to_uint.go


Will display the i variables types and the corresponding values.

Type: uint64