In this tutorial, we will learn about data types in Go. We will cover the basics of different data types available in Go, including integers, floats, strings, booleans, and complex numbers, as well as how to work with them.
Go supports several basic data types that are used to store different kinds of values. Knowing these data types is fundamental to working with Go.
The int
data type in Go is used to store signed integer values. It can be either 32 or 64 bits depending on the platform.
var x int = 42
fmt.Println(x)
fmt.Printf("%T", x)
The uint
data type is used to store unsigned integer values. It can also be either 32 or 64 bits depending on the platform.
var y uint = 42
fmt.Println(y)
fmt.Printf("%T", y)
The float32
and float64
data types are used to store floating-point numbers. float32
uses 32 bits, while float64
uses 64 bits.
var f32 float32 = 42.5
fmt.Println(f32)
fmt.Printf("%T", f32)
var f64 float64 = 42.5
fmt.Println(f64)
fmt.Printf("%T", f64)
The string
data type is used to store text or string values. Strings are enclosed in double quotes.
var s string = "Hello, Go!"
fmt.Println(s)
fmt.Printf("%T", s)
The bool
data type is used to store boolean values: true
or false
.
var flag bool = true
fmt.Println(flag)
fmt.Printf("%T", flag)
The complex64
and complex128
data types are used to store complex numbers. complex64
uses 64 bits, while complex128
uses 128 bits.
var c64 complex64 = 3 + 4i
fmt.Println(c64)
fmt.Printf("%T", c64)
var c128 complex128 = 3 + 4i
fmt.Println(c128)
fmt.Printf("%T", c128)
We can define and work with signed integer data types in Go.
For example,
x
and assign it an integer value of 42.x
to the console.Printf
function to determine the data type of x
and print the result.package main
import "fmt"
func main() {
var x int = 42
fmt.Println(x)
fmt.Printf("%T", x)
}
[1] 42 [1] int
We can define and work with unsigned integer data types in Go.
For example,
y
and assign it an integer value of 42.y
to the console.Printf
function to determine the data type of y
and print the result.package main
import "fmt"
func main() {
var y uint = 42
fmt.Println(y)
fmt.Printf("%T", y)
}
[1] 42 [1] uint
We can define and work with 32-bit floating-point data types in Go.
For example,
f32
and assign it a floating-point value of 42.5.f32
to the console.Printf
function to determine the data type of f32
and print the result.package main
import "fmt"
func main() {
var f32 float32 = 42.5
fmt.Println(f32)
fmt.Printf("%T", f32)
}
[1] 42.5 [1] float32
We can define and work with 64-bit floating-point data types in Go.
For example,
f64
and assign it a floating-point value of 42.5.f64
to the console.Printf
function to determine the data type of f64
and print the result.package main
import "fmt"
func main() {
var f64 float64 = 42.5
fmt.Println(f64)
fmt.Printf("%T", f64)
}
[1] 42.5 [1] float64
We can define and work with string data types in Go.
For example,
s
and assign it a string value of "Hello, Go!".s
to the console.Printf
function to determine the data type of s
and print the result.package main
import "fmt"
func main() {
var s string = "Hello, Go!"
fmt.Println(s)
fmt.Printf("%T", s)
}
[1] Hello, Go! [1] string
We can define and work with boolean data types in Go.
For example,
flag
and assign it a value of true
.flag
to the console.Printf
function to determine the data type of flag
and print the result.package main
import "fmt"
func main() {
var flag bool = true
fmt.Println(flag)
fmt.Printf("%T", flag)
}
[1] true [1] bool
We can define and work with 64-bit complex data types in Go.
For example,
c64
and assign it a value of 3 + 4i.c64
to the console.Printf
function to determine the data type of c64
and print the result.package main
import "fmt"
func main() {
var c64 complex64 = 3 + 4i
fmt.Println(c64)
fmt.Printf("%T", c64)
}
[1] (3+4i) [1] complex64
We can define and work with 128-bit complex data types in Go.
For example,
c128
and assign it a value of 3 + 4i.c128
to the console.Printf
function to determine the data type of c128
and print the result.package main
import "fmt"
func main() {
var c128 complex128 = 3 + 4i
fmt.Println(c128)
fmt.Printf("%T", c128)
}
[1] (3+4i) [1] complex128