Go is a statically typed language, which means it knows what types of values are, even before our program runs. Functions expect their arguments to be of a particular data type, and so do their return values. So, if you use any wrong type value in the wrong place, Go will let you know about it.
Golang data types are divided into two categories.
- Basic Data Types
- Composite Data Types
Basic Data Types include the following.
Whereas Composite Data Types contains
In this post, we will be looking at the Basic Datatypes. We can view any value type by using the reflect package TypeOf function and the size of any value using the unsafe package SizeOf function.
Integer
Integers data types will hold a whole number value like 134, 5, etc. They are of two kinds – signed and unsigned integers.
- Unsigned integers – Unsigned integers contain only the positive numbers ( including 0 ), and their types include uint8, uint16, uint32, uint64
- Signed integers – Signed integers have both positive and negative type integers, and their types include int8, int 16, int 32, int 64
where 8,16,32,64 represent the bits of each type. There are three other platform-dependent integer types – uint, int, and uintptr.
Golang Data Type | Size of the Data Type | Range of the Data Type |
---|---|---|
int / uint | 32 bits or 64 bits ( Depending on the machine) | Dependent on the machine |
int8 / uint8 | 8 bits ( 1 byte ) | int8 -27 to 27-1 uint8 0 to 28-1 |
int16 / uint16 | 16 bits ( 2 bytes) | int16 -215 to 215-1 uint16 0 to 216-1 |
int32 / uint32 | 32 bits ( 4 bytes) | int32 -231 to 231-1 uint32 0 to 232-1 |
int64 / uint64 | 64 bits ( 4 bytes) | int64 -263 to 263-1 uint64 0 to 264-1 |
int and uint data type
Both are platform-dependent means the size of these data types will depend on the machine or the platform. So, on the 32-bit machine, the size of int and uint will be 32-bit, and on a 64-bit machine, it would be 64-bit.
package main
import (
"fmt"
"reflect"
"unsafe"
)
func main() {
var i int = -3
var j uint = 5
// to print the value of the variable
fmt.Println("Value of i:", i)
fmt.Println("Value of j:", j)
// to print the data type of the variable
fmt.Println("Type of i:", reflect.TypeOf(i))
fmt.Println("Type of j:", reflect.TypeOf(j))
// to print the size of the variable in bytes
fmt.Println("Size of i in bytes:", unsafe.Sizeof(i))
fmt.Println("Size of j in bytes:", unsafe.Sizeof(j))
}
Output –
Value of i: -3
Value of j: 5
Type of i: int
Type of j: uint
Size of i in bytes: 8
Size of j in bytes: 8
int8 and uint8 data type
Their size will always be 8 bits or 1 byte, irrespective of the machine or platform.
- Range of int8 – The range of int8 is -27 to 27-1
- Range of uint8 – The range of uint8 is 0 to 28-1
package main
import (
"fmt"
"reflect"
"unsafe"
)
func main() {
var i int8 = 127
var j uint8 = 255
// to print the value of the variable
fmt.Println("Value of i:", i)
fmt.Println("Value of j:", j)
// to print the data type of the variable
fmt.Println("Type of i:", reflect.TypeOf(i))
fmt.Println("Type of j:", reflect.TypeOf(j))
// to print the size of the variable in bytes
fmt.Println("Size of i in bytes:", unsafe.Sizeof(i))
fmt.Println("Size of j in bytes:", unsafe.Sizeof(j))
}
Output –
Value of i: 127
Value of j: 255
Type of i: int8
Type of j: uint8
Size of i in bytes: 1
Size of j in bytes:: 1
int16 and unit16 data type
Their size is 16 bits or 2 bytes.
- Range of int16 – The range of int16 is -215 to 215-1
- Range of uint16 – The range of uint16 is 0 to 216-1
package main
import (
"fmt"
"reflect"
"unsafe"
)
func main() {
var i int16 = -1233
var j uint16 = 512
// to print the value of the variable
fmt.Println("Value of i:", i)
fmt.Println("Value of j:", j)
// to print the data type of the variable
fmt.Println("Type of i:", reflect.TypeOf(i))
fmt.Println("Type of j:", reflect.TypeOf(j))
// to print the size of the variable in bytes
fmt.Println("Size of i in bytes:", unsafe.Sizeof(i))
fmt.Println("Size of j in bytes:", unsafe.Sizeof(j))
}
Output –
Value of i: -1233
Value of j: 512
Type of i: int16
Type of j: uint16
Size of i in bytes: 2
Size of j in bytes: 2
int32 and unit32 data type
Their size is 32 bits or 4 bytes.
- Range of int32 – The range of int32 is -231 to 231-1
- Range of uint32 – The range of uint32 is 0 to 232-1
package main
import (
"fmt"
"reflect"
"unsafe"
)
func main() {
var i int32 = -12322333
var j uint32 = 512342432
// to print the value of the variable
fmt.Println("Value of i:", i)
fmt.Println("Value of j:", j)
// to print the data type of the variable
fmt.Println("Type of i:", reflect.TypeOf(i))
fmt.Println("Type of j:", reflect.TypeOf(j))
// to print the size of the variable in bytes
fmt.Println("Size of i in bytes:", unsafe.Sizeof(i))
fmt.Println("Size of j in bytes:", unsafe.Sizeof(j))
}
Output –
Value of i: -12322333
Value of j: 512342432
Type of i: int32
Type of j: uint32
Size of i in bytes: 4
Size of j in bytes: 4
int64 and unit64 data type
Their size is 64 bits or 8 bytes.
- Range of int64 – The range of int64 is -263 to 263-1
- Range of uint64 – The range of uint64 is 0 to 264-1
package main
import (
"fmt"
"reflect"
"unsafe"
)
func main() {
var i int64 = -1232323222333
var j uint64 = 5123432323432
// to print the value of the variable
fmt.Println("Value of i:", i)
fmt.Println("Value of j:", j)
// to print the data type of the variable
fmt.Println("Type of i:", reflect.TypeOf(i))
fmt.Println("Type of j:", reflect.TypeOf(j))
// to print the size of the variable in bytes
fmt.Println("Size of i in bytes:", unsafe.Sizeof(i))
fmt.Println("Size of j in bytes:", unsafe.Sizeof(j))
}
Output –
Value of i: -1232323222333
Value of j: 5123432323432
Type of i: int64
Type of j: uint64
Size of i in bytes: 8
Size of j in bytes: 8
Floating point numbers
Floating point numbers are numbers that represent the decimal point numbers. For example, 123.45, -12.23, 0.1234 are all floating-point numbers. Two data types represent floating point numbers –
- Float32
- Float64
Float64 is the default data type assigned to a variable containing decimal points if we don’t assign the data type ourselves, as shown in the program below.
import (
"fmt"
"reflect"
)
func main() {
var i = -12323.13
// to print the data type of the variable
fmt.Println("Type of i:", reflect.TypeOf(i))
}
Output –
Type of i: float64
float32 data type
Its size is 32 bits or 4 bytes. A 32-bit floating number is a single-precision floating-point number. Out of those 32 bits –
- 1 bit is kept for the sign ( positive or negative ).
- 8 bits for the exponent.
- And 23 bits for the mantissa are the significant digits in the number.
import (
"fmt"
"reflect"
"unsafe"
)
func main() {
var i float32 = -12323.13
// to print the value of the variable
fmt.Println("Value of i:", i)
// to print the data type of the variable
fmt.Println("Type of i:", reflect.TypeOf(i))
// to print the size of the variable in bytes
fmt.Println("Size of i in bytes:", unsafe.Sizeof(i))
}
Output –
Value of i: -12323.13
Type of i: float32
Size of i in bytes: 4
float64 data type
Its size is 64 bits or 8 bytes. A 64-bit floating-point number is a double-precision floating-point number. Out of the 64 bits –
- 1 bit is kept for the sign ( positive or negative ).
- 11 bits for the exponent.
- And 52 bits for the mantissa are the significant digits in the number.
package main
import (
"fmt"
"reflect"
"unsafe"
)
func main() {
var i float64 = -123234422423223323.13
// to print the value of the variable
fmt.Println("Value of i:", i)
// to print the data type of the variable
fmt.Println("Type of i:", reflect.TypeOf(i))
// to print the size of the variable in bytes
fmt.Println("Size of i in bytes:", unsafe.Sizeof(i))
}
Output –
Value of i: -1.2323442242322333e+17
Type of i: float64
Size of i in bytes: 8
Complex numbers
A Complex number is a number of the form a + bi. There are two data types of complex numbers in go language –
- complex64 ( Here, both a and b are of float32 type ).
- complex128 ( Here, both a and b are of float64 type ).
complex64 data type
Its both real and imaginary parts are of float32 type. So, if we try to use float64 on it, the program will throw an error, as shown below.
package main
import (
"fmt"
"reflect"
"unsafe"
)
func main() {
var a float64 = 5 // using float64 instead of float32
var b float64 = 6 // using float64 instead of float32
var i complex64 = complex(a, b)
// to print the value of the variable
fmt.Println("Value of i:", i)
// to print the data type of the variable
fmt.Println("Type of i:", reflect.TypeOf(i))
// to print the size of the variable in bytes
fmt.Println("Size of i in bytes:", unsafe.Sizeof(i))
}
Output –
cannot use complex(a, b) (type complex128) as type complex64 in assignment
So, we have to use the float32 type with complex64.
package main
import (
"fmt"
"reflect"
"unsafe"
)
func main() {
var a float32 = 5
var b float32 = 6
var i complex64 = complex(a, b)
// to print the value of the variable
fmt.Println("Value of i:", i)
// to print the data type of the variable
fmt.Println("Type of i:", reflect.TypeOf(i))
// to print the size of the variable in bytes
fmt.Println("Size of i in bytes:", unsafe.Sizeof(i))
}
Output –
Value of i: (5+6i)
Type of i: complex64
Size of i in bytes: 8
complex128 data type
Here both real and imaginary parts should be of float64 type.
package main
import (
"fmt"
"reflect"
"unsafe"
)
func main() {
var a float64 = 5
var b float64 = 6
var i complex128 = complex(a, b)
// to print the value of the variable
fmt.Println("Value of i:", i)
// to print the data type of the variable
fmt.Println("Type of i:", reflect.TypeOf(i))
// to print the size of the variable in bytes
fmt.Println("Size of i in bytes:", unsafe.Sizeof(i))
}
Output –
Value of i: (5+6i)
Type of i: complex128
Size of i in bytes: 16
byte
A byte can help in representing the ASCII characters in go. In the go language, a byte is an alias for uint8. So, the size for byte is also 1 byte or 8 bits and ranges from 0 to 255.
package main
import (
"fmt"
"reflect"
"unsafe"
)
func main() {
var i byte = 'a'
// to print the value of the variable
fmt.Println("Value of i:", i)
// to print the data type of the variable
fmt.Println("Type of i:", reflect.TypeOf(i))
// to print the size of the variable in bytes
fmt.Println("Size of i in bytes:", unsafe.Sizeof(i))
}
Output –
Value of i: 97
Type of i: uint8
Size of i in bytes: 1
You can see here that the type of i is shown as uint8 because, under the hood, a byte is nothing but uint8 itself.
rune
The rune is an alias for int32. So, its size is also 32 bits or 4 bytes. It is mainly used to store the Unicode code points value. For example, the Unicode code point for lowercase a is U+0061 (the decimal value is 97 ).
package main
import (
"fmt"
"reflect"
"unsafe"
)
func main() {
var i rune = 'a'
// to print the value of the variable
fmt.Println("Value of i:", i)
// to print the data type of the variable
fmt.Println("Type of i:", reflect.TypeOf(i))
// to print the size of the variable in bytes
fmt.Println("Size of i in bytes:", unsafe.Sizeof(i))
}
Output –
Value of i: 97
Type of i: int32
Size of i in bytes: 4
We used rune as our data type, but the type printed as int32.
Booleans
The boolean data type is represented as bool and can have only two values, true or false. Boolean is mainly used to validate whether a condition holds true or not. Its size is 1 byte only.
package main
import (
"fmt"
"reflect"
"unsafe"
)
func main() {
var i bool = true
// to print the value of the variable
fmt.Println("Value of i:", i)
// to print the data type of the variable
fmt.Println("Type of i:", reflect.TypeOf(i))
// to print the size of the variable in bytes
fmt.Println("Size of i in bytes:", unsafe.Sizeof(i))
}
Output –
Value of i: true
Type of i: bool
Size of i in bytes: 1
We have written an altogether different article for strings. Please go to this link to learn more about strings.
We hope you have liked our article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.