There are a total of 19 basic types in Go or Golang. Those are mentioned below:
boolstringintint8int16int32int64uintuint8uint16uint32uint64uintptrbyterunefloat32float64complex64complex128
bool type
The bool type is used for holding truthy/falsy values such as true or false.
var isAdmin bool // isAdmin variable has a type of `bool`
isAdmin = true
string type
The string type is used to hold a sequence of characters. The characters are enclosed in double quotation marks symbol ("").
var name string // name variable has a type of `string`
name = "John Doe"
int, int8, int16, int32, and int64 type
The int types are used to hold numerical values including the negative and non-negative numbers.
The numbers after the word int refer to the size. For example, the int8 type can hold values of 8-bit size.
Each of the int types can have negative and non-negative numbers up to a certain range. These are mentioned below:
- The
int8type can have numbers ranging from-128to127. - The
int16type can have numbers ranging from-32768to32767. - The
int32type can have numbers ranging from-2147483648to2147483647. - The
int64type can have numbers ranging from-2^63(-2 to the power of 63) to2^63 - 1(2 to the power of 63 minus 1). - The
inttype is a platform-dependent type which means that on a32bit system it will be the same as theint32type and on a64bit system it will be the same as theint64type.
var num int8 // num variable has a type of `int8`
num = -128
uint, uint8, uint16, uint32, and uint64 type
The uint types are used to hold numerical values for non-negative numbers. It is also called as unsigned integer type.
The numbers after the word int refer to the size. For example, the uint8 type can hold values of 8-bit size.
Each of the uint types can have non-negative numbers up to a certain range. These are mentioned below:
- The
uint8type can have numbers ranging from0to255. - The
uint16type can have numbers ranging from0to65535. - The
uint32type can have numbers ranging from0to4294967295. - The
uint64type can have numbers ranging from0to2^64 - 1(2 to the power of 64 minus 1). - The
uinttype is a platform-dependent type which means that on a32bit system it will be the same as theuint32type and on a64bit system it will be the same as theuint64type.
var num uint8 // num variable has a type of `uint8`
num = 255
uintptr type
You may never have to use the uintptr type unless you are developing code related to Go runtime libraries. It is used to bypass the Go's type system and can be used as the reference of memory locations of C or other system-level programming languages codes.
byte type
The byte is an alias for the uint8 type.
rune type
The rune is an alias for the int32 type.
One cool thing with the rune type is that if you assign a Unicode symbol like 😃 (Smiling Face with Open Mouth) to the variable, then it will be automatically converted into its hexadecimal Unicode codepoint. The hexadecimal is essentially an integer.
var favEmoji rune
favEmoji = '😃'
fmt.Println(favEmoji) // 128515
float32 and float64 type
The float32 and float64 type is used to hold numerical values having a decimal or fractional part.
- The
float32type has a32bit size and has single precision. - The
float64type has a64bit size and has double precision.
floatNum := 78.65 // floatNum variable has the type of float64
complex64 and complex128 type
Go can easily handle complex numbers also with the complex64 and complex128 types.
- The
complex64type has a real part composed of thefloat32type and an imaginary part composed of thefloat32type. - The
complex128type has a real part composed of thefloat64type and an imaginary part composed of thefloat64type.
It can be done like this,
complexNum := 5 + 8i // complexNum variable type is complex128
Or you can use the built-in complex() function to create the real and imaginary parts of a complex number like this,
complexNum := complex(5, 8) // complexNum variable type is complex128
fmt.println(complexNum) // 5 + 8i
These are the available basic types in Golang.
That's all 😃!