Data types play an important role in determining what type of data will be kept in computer memory for programming purposes. This tutorial will give you an understanding of data types in Go programming.
What Are Data Types in Go Programming?
Go data types are a fundamental component determining the size and type of variables that store program values. Go is a statically-typed language, meaning once the data type of a variable gets defined, it can only store data of that type.
- Primary Data Types (Also called Built-in, Predefined, and Basic Data Types)
- Numeric Types
- Integer Type
- Floating Type
- String Type
- Boolean Type
- Numeric Types
- Derived Data Types
Let us now explore the Pre-defined data types in detail.
Go Integer Data Types
Integer data types can store whole numbers with a specific range of values, e.g., 50
, -35
, 123450
, etc. It can be signed and unsigned, where signed integers can hold positive and negative values, while unsigned integers can only hold non-negative values.
Keyword | Category | Type/Size |
---|---|---|
uint
| Unsigned | It is dependent according to 32-bit and 64-bit Operating systems. (0 to 4294967295) in 32-bit systems and (0 to 18446744073709551615) in 64-bit systems. |
uint8
| Unsigned | 8-bit integers (0 to 255). |
uint16
| Unsigned | 16-bit integers (0 to 65535). |
uint32
| Unsigned | 32-bit integers (0 to 4294967295). |
uint64
| Unsigned | 64-bit integers (0 to 18446744073709551615). |
uintptr
| Unsigned | An unsigned integer that can hold bits of pointer values. |
int
| Signed | It is dependent according to 32-bit and 64-bit Operating systems. (-2147483648 to 2147483647) in 32-bit systems and (-9223372036854775808 to 9223372036854775807) in 64-bit systems. |
int8
| Signed | 8-bit integers (-128 to 127). |
int16
| Signed | 16-bit integers (-32768 to 32767). |
int32
| Signed | 32-bit integers (-2147483648 to 2147483647). |
int64
| Signed | 64-bit integers (-9223372036854775808 to 9223372036854775807). |
Example Program:
/* Author: w3schools.in
Date: 2022-12-13
Description: Example "go program" to print integer */
package main
import "fmt"
func main() {
var n int = 23
fmt.Println("The Integer value is ", n)
}
Output:
The Integer value is 23
Unless there is a specific need in the program, the int
keyword is typically used by default to create integers.
Go Floating Data Type
The float type is a numeric data type that can store values with decimals and values having mantissa and exponent parts. These data types can also store real and imaginary numbers.
Keyword | Type/Size |
---|---|
float32
| IEEE-754 32-bit floating-point numbers. |
float64
| IEEE-754 64-bit floating-point numbers. |
complex64
| Complex numbers with float32 as the real part and the other as imaginary.
|
complex128
| Complex numbers with float64 as the real part and the other as imaginary.
|
Example Program:
/* Author: w3schools.in
Date: 2022-12-13
Description: Example "go program" to print float */
package main
import "fmt"
func main() {
var n float32 = 3.1625
fmt.Println("The Floating point value is ", n)
}
Output:
The Floating point value is 3.1625
Example Program:
/* Author: w3schools.in
Date: 2022-12-13
Description: Example "go program" to print float */
package main
import "fmt"
func main() {
var g complex128 = complex(8, 3)
var k complex64 = complex(6, 3)
fmt.Println(g)
fmt.Println(k)
fmt.Printf("The type of first variable is %T and "+ "the type of second variable is %T", g, k)
}
Output:
D:\Go-Example>go run filename.go
(8+3i)
(6+3i)
The type of first variable is complex128 and the type of second variable is complex64
Go String Data Type
String data type represents the set of string values that are characters enclosed with double quotes. The value it holds is a sequence of bytes. Go strings are immutable data types, meaning that once created, their value cannot be changed.
Example Program:
/* Author: w3schools.in
Date: 2022-12-13
Description: Example "go program" to print string */
package main
import "fmt"
func main() {
var emp_name string = "Alex"
fmt.Println("The name of the employee as string value is", emp_name)
}
Output:
The name of the employee as string value is Alex
Go Boolean Data Type
The Boolean data type allows a variable to store true
or false
and has a default value of false
.
Example Program:
/* Author: w3schools.in
Date: 2022-12-13
Description: Example "go program" to use boolean */
package main
import "fmt"
func main() {
var b bool = true //Boolean value can only be true or false
fmt.Println("The Boolean value is", b)
}
Output:
The Boolean value is true
Boolean values are mainly used in conditional checking.
There are a few other data types, such as:
Data Types | Description |
---|---|
Byte
| Byte data type stores non-negative integers. For example, 6, 98, etc. |
Rune
| Rune data type stores characters and is used as a 32-bit integer internally. For example: 'g', '2', '>', etc. |
Go Derived Data Types
Apart from all these basic data types, specific categories of data types are formed from pre-defined data types but have different functionalities. These are:
- Pointer
- Array
- Structure
- Union
- Function
- Slice
- Interface
- Map
- Channel
Since these are separate topics, they are beyond the scope of this tutorial. These are explained in detail in subsequent tutorials.