Code With Go
Twitter
Web
Blog
LinkedIn
Search…
Code With Go
01. What, Who, Why and Where?
02. Compiled vs Interpreted languages
03. Types of Type!
04. Stack and Heap Memory
05. Garbage Collection
06. About Go
07. Go Playground
08. Hello, World!
09. Installing Go
10. Variables
11. Constants
12. Integers
12. Floats
13. Boolean
14. Strings
15. Complex Numbers
16. If Else
17. Switch
18. For
19. User Defined Types
20. Arrays
21. Slices
22. Structs
23. Maps
24. Functions
25. Defer
26. Pointers
27. Methods
Resources
Resources - Paid
Resources - Free
Powered By
GitBook
12. Integers
Using Natural Numbers
Integer Types
Integers are used to store natural numbers are 1,2,3.
Go has 8 types on integers
int8
,
int16
,
int32
, and
int64
, and corresponding unsigned versions
uint8
,
uint16
,
uint32
, and
uint64.
unsigned integers can store positive numbers only.
8,16,32 and 64 represents how many bits are used to store value.
Bits
Signed
Unsigned
8
–128 to 127
0 to 255
16
–32,768 to 32,767
0 to 65535
32
–2,147,483,648 to 2,147,483,647
0 to 4,294,967,295
64
–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
0 to 18,446,744,073,709,551,615
Declaring Integers
Integers are declared using one of the 3 ways:
main.go
1
var
j
int32
=
20
2
var
i
int
=
10
3
var
i
=
10
4
i
:=
10
5
var
k
int
Copied!
If we are not specifying type like in 2nd and 3rd declaration, it will default to
int
Zero value for integer is
0
Always use short declaration way when assigning initial value. If you wish to initialize a variable to its zero value, use var declaration.
Using Test Driven Development for integers
Learn how to do test driven development when using integers from Chris
learn-go-with-tests/integers.md at master · quii/learn-go-with-tests
GitHub
Previous
11. Constants
Next
12. Floats
Last modified
2yr ago
Copy link
Contents
Integer Types
Declaring Integers
Using Test Driven Development for integers