How to convert an int type value to a uint type value in Go or Golang?

Published August 18, 2022

To convert an int type value to a uint type value in Go or Golang, you can use the built-in uint() function and pass the int type value as an argument to the function. The function returns the value as the uint type.

For example, let's say we need to convert an int type value of 3000 to a uint type.

To do that we can use the uint() built-in function and pass the value of 3000 as an argument to the function like this,

package main

import "fmt"

func main() {
    // a `int` type value
    intNum := 3000

    // convert the `int` type value of 3000
    // to `uint` type value using the
    // `uint()` built-in method
    uintNum := uint(intNum)

    // log to the console
    fmt.Println(uintNum) // `3000` and the type is `uint`
}

As you can see from the above code we have converted the int type value of 3000 to a uint type value. Yay 🥳!

See the above code live in The Go Playground.

That's all 😃.

Feel free to share if you found this useful 😃.


Share on: Facebook Twitter