how to change uint size in golang?

Hi Friends 👋,

Welcome To aGuideHub! ❤️

This article will help you to change your unsigned integer size like changing any uint to uint8, any uint to uint16, any uint to uint32, any uint to uint64, and any uint to uint.

Table of content

Convert to uint

  • uint8 to uint
  • uint16 to uint
  • uint32 to uint
  • uint46 to uint

Convert to uint8

  • uint to uint8
  • uint16 to uint8
  • uint32 to uint8
  • uint64 to uint8

Convert to uint16

  • uint to uint16
  • uint8 to uint16
  • uint32 to uint16
  • uint64 to uint16

Convert to uint32

  • uint to uint32
  • uint8 to uint32
  • uint16 to uint32
  • uint64 to uint32

Convert to uint64

  • uint to uint64
  • uint8 to uint64
  • uint16 to uint64
  • uint32 to uint64

Important Points

Difference between int and uint

UInt does not allow for negative numbers.

uint’s and int’s max value

uint8  : 0 to 255 
uint16 : 0 to 65535 
uint32 : 0 to 4294967295 
uint64 : 0 to 18446744073709551615 
int8   : -128 to 127 
int16  : -32768 to 32767 
int32  : -2147483648 to 2147483647 
int64  : -9223372036854775808 to 9223372036854775807

Today, I will show you how do I change the size of the uint in golang, as above mentioned I’m going to explain all the conversions between uint.

Let’s start our Golang change size of the uint example

Convert to uint example

To any uint ( uint8, uint16, uint32, uint64 ) to uint in golang, use the uint() method it will make any uint to uint.

main.go

package main

import (
  "fmt"
  "reflect"
)

func main() {
  var i8 uint8 = 126
  var i16 uint16 = 32766
  var i32 uint32 = 2147483646
  var i64 uint64 = 9223372036854775806
  fmt.Println(i8, reflect.TypeOf(i8))
  fmt.Println(i16, reflect.TypeOf(i16))
  fmt.Println(i32, reflect.TypeOf(i32))
  fmt.Println(i64, reflect.TypeOf(i64))

  uint1 := uint(i8)
  fmt.Println(uint1, reflect.TypeOf(uint1))

  uint2 := uint(i16)
  fmt.Println(uint2, reflect.TypeOf(uint2))

  uint3 := uint(i32)
  fmt.Println(uint3, reflect.TypeOf(uint3))

  uint4 := uint(i64)
  fmt.Println(uint4, reflect.TypeOf(uint4))
}

In the above example, we have converted any uint to uint and printed it in the golang console. let’s check the output.

Output

126 uint8
32766 uint16
2147483646 uint32
9223372036854775806 uint64
126 uint
32766 uint
2147483646 uint
9223372036854775806 uint

Try it Yourself

Convert to uint8 example

To any uint ( uint, uint16, uint32, uint64 ) to uint8 in golang, use the uint8() method it will make any uint to uint8.

main.go

package main

import (
  "fmt"
  "reflect"
)

func main() {
  var i uint = 126
  var i16 uint16 = 32766
  var i32 uint32 = 2147483646
  var i64 uint64 = 9223372036854775806
  fmt.Println(i, reflect.TypeOf(i))
  fmt.Println(i16, reflect.TypeOf(i16))
  fmt.Println(i32, reflect.TypeOf(i32))
  fmt.Println(i64, reflect.TypeOf(i64))

  uint81 := uint8(i)
  fmt.Println(uint81, reflect.TypeOf(uint81))

  // below conversion does not fit in uint8, I will recommend using uint for this
  uint82 := uint8(i16)
  fmt.Println(uint82, reflect.TypeOf(uint82))

  uint83 := uint8(i32)
  fmt.Println(uint83, reflect.TypeOf(uint83))

  uint84 := uint8(i64)
  fmt.Println(uint84, reflect.TypeOf(uint84))
}

In the above example, we have converted any uint to uint8 and printed it in the golang console.

Some conversions will not fit in uint8, where you should use the uint() conversion.

let’s check the output.

Output

126 uint
32766 uint16
2147483646 uint32
9223372036854775806 uint64
126 uint8
254 uint8
254 uint8
254 uint8

Try it Yourself

Convert to uint16 example

To any uint ( uint, uint8, uint32, uint64 ) to uint16 in golang, use the uint16() method it will make any uint to uint16.

main.go

package main

import (
  "fmt"
  "reflect"
)

func main() {
  var i uint = 1264
  var i8 uint8 = 126
  var i32 uint32 = 2147483646
  var i64 uint64 = 9223372036854775806
  fmt.Println(i, reflect.TypeOf(i))
  fmt.Println(i8, reflect.TypeOf(i8))
  fmt.Println(i32, reflect.TypeOf(i32))
  fmt.Println(i64, reflect.TypeOf(i64))

  uint161 := uint16(i)
  fmt.Println(uint161, reflect.TypeOf(uint161))

  uint162 := uint16(i8)
  fmt.Println(uint162, reflect.TypeOf(uint162))

  // below conversion does not fit in uint8, I will recommend using uint for this

  uint163 := uint16(i32)
  fmt.Println(uint163, reflect.TypeOf(uint163))

  uint164 := uint16(i64)
  fmt.Println(uint164, reflect.TypeOf(uint164))
}

In the above example, we have converted any uint to uint16 and printed it in the golang console.

Some conversions will not fit in uint16, where you should use the uint() conversion.

let’s check the output.

Output

Output:

1264 uint
126 uint8
2147483646 uint32
9223372036854775806 uint64
1264 uint16
126 uint16
65534 uint16
65534 uint16

Try it Yourself

Convert to uint32 example

To any uint ( uint, uint8, uint16, uint64 ) to uint32 in golang, use the uint32() method it will make any uint to uint32.

main.go

package main

import (
  "fmt"
  "reflect"
)

func main() {
  var i uint = 1264
  var i8 uint8 = 126
  var i16 uint16 = 32766
  var i64 uint64 = 9223372036854775806
  fmt.Println(i, reflect.TypeOf(i))
  fmt.Println(i8, reflect.TypeOf(i8))
  fmt.Println(i16, reflect.TypeOf(i16))
  fmt.Println(i64, reflect.TypeOf(i64))

  uint321 := uint32(i)
  fmt.Println(uint321, reflect.TypeOf(uint321))

  uint322 := uint32(i8)
  fmt.Println(uint322, reflect.TypeOf(uint322))

  uint323 := uint32(i16)
  fmt.Println(uint323, reflect.TypeOf(uint323))

  // below conversion does not fit in uint8, I will recommend using uint for this
  uint324 := uint32(i64)
  fmt.Println(uint324, reflect.TypeOf(uint324))
}

In the above example, we have converted any uint to uint32 and printed it in the golang console.

Some conversions will not fit in uint32, where you should use the uint() conversion.

let’s check the output.

Output

Output:

1264 uint
126 uint8
32766 uint16
9223372036854775806 uint64
1264 uint32
126 uint32
32766 uint32
4294967294 uint32

Try it Yourself

Convert to uint64 example

To any uint ( uint, uint8, uint16, uint32 ) to uint64 in golang, use the uint64() method it will make any uint to uint64.

main.go

package main

import (
  "fmt"
  "reflect"
)

func main() {
  var i uint = 1264
  var i8 uint8 = 126
  var i16 uint16 = 32766
  var i32 uint32 = 2147483646
  fmt.Println(i, reflect.TypeOf(i))
  fmt.Println(i8, reflect.TypeOf(i8))
  fmt.Println(i16, reflect.TypeOf(i16))
  fmt.Println(i32, reflect.TypeOf(i32))

  uint641 := uint64(i)
  fmt.Println(uint641, reflect.TypeOf(uint641))

  uint642 := uint64(i8)
  fmt.Println(uint642, reflect.TypeOf(uint642))

  uint643 := uint64(i16)
  fmt.Println(uint643, reflect.TypeOf(uint643))

  uint644 := uint64(i32)
  fmt.Println(uint644, reflect.TypeOf(uint644))
}

In the above example, we have converted any uint to uint64 and printed it in the golang console.

Some conversions will not fit in uint64, where you should use the uint() conversion.

let’s check the output.

Output

Output:

1264 uint
126 uint8
32766 uint16
2147483646 uint32
1264 uint64
126 uint64
32766 uint64
2147483646 uint64

Try it Yourself

I hope it helps you, All the best 👍.