how to change int size in golang?

Hi Friends 👋,

Welcome To aGuideHub! ❤️

This article will help you to change your integer sizes like changing any int to int8, any int to int16, any int to int32, any int to int64, and any int to int.

Table of content

Convert to int

  • int8 to int
  • int16 to int
  • int32 to int
  • int46 to int

Convert to int8

  • int to int8
  • int16 to int8
  • int32 to int8
  • int64 to int8

Convert to int16

  • int to int16
  • int8 to int16
  • int32 to int16
  • int64 to int16

Convert to int32

  • int to int32
  • int8 to int32
  • int16 to int32
  • int64 to int32

Convert to int64

  • int to int64
  • int8 to int64
  • int16 to int64
  • int32 to int64

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 int in golang, as above mentioned I’m going to explain all the conversions between int.

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

Convert to int example

To any int ( int8, int16, int32, int64 ) to int in golang, use the int() method it will make any int to int.

main.go

package main

import (
  "fmt"
  "reflect"
)

func main() {
  var i8 int8 = 126
  var i16 int16 = 32766
  var i32 int32 = 2147483646
  var i64 int64 = 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))

  int1 := int(i8)
  fmt.Println(int1, reflect.TypeOf(int1))

  int2 := int(i16)
  fmt.Println(int2, reflect.TypeOf(int2))

  int3 := int(i32)
  fmt.Println(int3, reflect.TypeOf(int3))

  int4 := int(i64)
  fmt.Println(int4, reflect.TypeOf(int4))
}

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

Output

126 int8
32766 int16
2147483646 int32
9223372036854775806 int64
126 int
32766 int
2147483646 int
9223372036854775806 int

Try it Yourself

Convert to int8 example

To any int ( int, int16, int32, int64 ) to int8 in golang, use the int8() method it will make any int to int8.

main.go

package main

import (
  "fmt"
  "reflect"
)

func main() {
  var i int = 126
  var i16 int16 = 32766
  var i32 int32 = 2147483646
  var i64 int64 = 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))

  int81 := int8(i)
  fmt.Println(int81, reflect.TypeOf(int81))

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

  int83 := int8(i32)
  fmt.Println(int83, reflect.TypeOf(int83))

  int84 := int8(i64)
  fmt.Println(int84, reflect.TypeOf(int84))
}

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

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

let’s check the output.

Output

126 int
32766 int16
2147483646 int32
9223372036854775806 int64
126 int8
-2 int8
-2 int8
-2 int8

Try it Yourself

Convert to int16 example

To any int ( int, int8, int32, int64 ) to int16 in golang, use the int16() method it will make any int to int16.

main.go

package main

import (
  "fmt"
  "reflect"
)

func main() {
  var i int = 1264
  var i8 int8 = 126
  var i32 int32 = 2147483646
  var i64 int64 = 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))

  int161 := int16(i)
  fmt.Println(int161, reflect.TypeOf(int161))

  int162 := int16(i8)
  fmt.Println(int162, reflect.TypeOf(int162))

  // below conversion does not fit in int8, I will recommend using int for this

  int163 := int16(i32)
  fmt.Println(int163, reflect.TypeOf(int163))

  int164 := int16(i64)
  fmt.Println(int164, reflect.TypeOf(int164))
}

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

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

let’s check the output.

Output

Output:

1264 int
126 int8
2147483646 int32
9223372036854775806 int64
1264 int16
126 int16
-2 int16
-2 int16

Try it Yourself

Convert to int32 example

To any int ( int, int8, int16, int64 ) to int32 in golang, use the int32() method it will make any int to int32.

main.go

package main

import (
  "fmt"
  "reflect"
)

func main() {
  var i int = 1264
  var i8 int8 = 126
  var i16 int16 = 32766
  var i64 int64 = 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))

  int321 := int32(i)
  fmt.Println(int321, reflect.TypeOf(int321))

  int322 := int32(i8)
  fmt.Println(int322, reflect.TypeOf(int322))

  int323 := int32(i16)
  fmt.Println(int323, reflect.TypeOf(int323))

  // below conversion does not fit in int8, I will recommend using int for this
  int324 := int32(i64)
  fmt.Println(int324, reflect.TypeOf(int324))
}

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

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

let’s check the output.

Output

Output:

1264 int
126 int8
32766 int16
9223372036854775806 int64
1264 int32
126 int32
32766 int32
-2 int32

Try it Yourself

Convert to int64 example

To any int ( int, int8, int16, int32 ) to int64 in golang, use the int64() method it will make any int to int64.

main.go

package main

import (
  "fmt"
  "reflect"
)

func main() {
  var i int = 1264
  var i8 int8 = 126
  var i16 int16 = 32766
  var i32 int32 = 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))

  int641 := int64(i)
  fmt.Println(int641, reflect.TypeOf(int641))

  int642 := int64(i8)
  fmt.Println(int642, reflect.TypeOf(int642))

  int643 := int64(i16)
  fmt.Println(int643, reflect.TypeOf(int643))

  int644 := int64(i32)
  fmt.Println(int644, reflect.TypeOf(int644))
}

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

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

let’s check the output.

Output

Output:

1264 int
126 int8
32766 int16
2147483646 int32
1264 int64
126 int64
32766 int64
2147483646 int64

Try it Yourself

I hope it helps you, All the best 👍.