bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/Go/Go Tutorial
Go•Go Tutorial

Go Integer Data Types

Integer data types are used to store a whole number without decimals, like 35, -50, or 1345000.

The integer data type has two categories:

  • Signed integers - can store both positive and negative values
  • Unsigned integers - can only store non-negative values

Tip

The default type for integer is int . If you do not specify a type, the type will be int .

Signed Integers

Signed integers, declared with one of the int keywords, can store both positive and negative values:

Example

package main
import ("fmt")
func main() {
  var x int = 500
  var y int = -4500
  fmt.Printf("Type: %T, value: %v", x, x)
  fmt.Printf("Type: %T, value: %v", y, y)
}

Go has five keywords/types of signed integers:

TypeSizeRange
intDepends on platform: 32 bits in 32 bit systems and 64 bit in 64 bit systems-2147483648 to 2147483647 in 32 bit systems and -9223372036854775808 to 9223372036854775807 in 64 bit systems
int88 bits/1 byte-128 to 127
int1616 bits/2 byte-32768 to 32767
int3232 bits/4 byte-2147483648 to 2147483647
int6464 bits/8 byte-9223372036854775808 to 9223372036854775807

Unsigned Integers

Unsigned integers, declared with one of the uint keywords, can only store non-negative values:

Example

package main
import ("fmt")
func main() {
  var x uint = 500
  var y uint = 4500
  fmt.Printf("Type: %T, value: %v", x, x)
  fmt.Printf("Type: %T, value: %v", y, y)
}

Go has five keywords/types of unsigned integers:

TypeSizeRange
uintDepends on platform: 32 bits in 32 bit systems and 64 bit in 64 bit systems0 to 4294967295 in 32 bit systems and 0 to 18446744073709551615 in 64 bit systems
uint88 bits/1 byte0 to 255
uint1616 bits/2 byte0 to 65535
uint3232 bits/4 byte0 to 4294967295
uint6464 bits/8 byte0 to 18446744073709551615

Which Integer Type to Use?

The type of integer to choose, depends on the value the variable has to store.

Example

package main
import ("fmt")
func main() {
  var x int8 = 1000
  fmt.Printf("Type: %T, value: %v", x, x)
}

Previous

Go Boolean Data Type

Next

Go Float Data Types

This chapter

Overview
39

Lessons

95m

Read time

1. Go Tutorial2. Go Introduction3. Go Getting Started4. Go Syntax5. Go Comments6. Go Variables7. Go Multiple Variable Declaration8. Go Variable Naming Rules9. Go Constants10. Go Output Functions11. Go Formatting Verbs12. Go Data Types13. Go Boolean Data Type14. Go Integer Data Types15. Go Float Data Types16. Go String Data Type17. Go Arrays18. Go Slices19. Go Access, Change, Append and Copy Slices20. Go Operators21. Go Arithmetic Operators22. Go Assignment Operators23. Go Comparison Operators24. Go Logical Operators25. Go Bitwise Operators26. Go Conditions27. Go if statement28. Go if else Statement29. Go else if Statement30. Go Nested if Statement31. Go switch Statement32. Go Multi-case switch Statement33. Go For Loops34. Go Functions35. Go Function Parameters and Arguments36. Go Function Returns37. Go Recursion Functions38. Go Struct39. Go Maps

On this page

Signed IntegersUnsigned IntegersWhich Integer Type to Use?