Data Types in Go
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.
Understanding Data Types in Go
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.
Integer Data Type
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)Unsigned Integer Data Type
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)Float Data Type
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)String Data Type
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)Boolean Data Type
The bool data type is used to store boolean values: true or false.
var flag bool = true
fmt.Println(flag)
fmt.Printf("%T", flag)Complex Data Type
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)Example 1: Working with Integer Data Type
We can define and work with signed integer data types in Go.
For example,
- Create an integer variable named
xand assign it an integer value of 42. - Print the value of
xto the console. - Use the
Printffunction to determine the data type ofxand print the result.
Go Program
package main
import "fmt"
func main() {
var x int = 42
fmt.Println(x)
fmt.Printf("%T", x)
}Output
[1] 42 [1] int
Example 2: Working with Unsigned Integer Data Type
We can define and work with unsigned integer data types in Go.
For example,
- Create an unsigned integer variable named
yand assign it an integer value of 42. - Print the value of
yto the console. - Use the
Printffunction to determine the data type ofyand print the result.
Go Program
package main
import "fmt"
func main() {
var y uint = 42
fmt.Println(y)
fmt.Printf("%T", y)
}Output
[1] 42 [1] uint
Example 3: Working with Float32 Data Type
We can define and work with 32-bit floating-point data types in Go.
For example,
- Create a float32 variable named
f32and assign it a floating-point value of 42.5. - Print the value of
f32to the console. - Use the
Printffunction to determine the data type off32and print the result.
Go Program
package main
import "fmt"
func main() {
var f32 float32 = 42.5
fmt.Println(f32)
fmt.Printf("%T", f32)
}Output
[1] 42.5 [1] float32
Example 4: Working with Float64 Data Type
We can define and work with 64-bit floating-point data types in Go.
For example,
- Create a float64 variable named
f64and assign it a floating-point value of 42.5. - Print the value of
f64to the console. - Use the
Printffunction to determine the data type off64and print the result.
Go Program
package main
import "fmt"
func main() {
var f64 float64 = 42.5
fmt.Println(f64)
fmt.Printf("%T", f64)
}Output
[1] 42.5 [1] float64
Example 5: Working with String Data Type
We can define and work with string data types in Go.
For example,
- Create a string variable named
sand assign it a string value of "Hello, Go!". - Print the value of
sto the console. - Use the
Printffunction to determine the data type ofsand print the result.
Go Program
package main
import "fmt"
func main() {
var s string = "Hello, Go!"
fmt.Println(s)
fmt.Printf("%T", s)
}Output
[1] Hello, Go! [1] string
Example 6: Working with Boolean Data Type
We can define and work with boolean data types in Go.
For example,
- Create a boolean variable named
flagand assign it a value oftrue. - Print the value of
flagto the console. - Use the
Printffunction to determine the data type offlagand print the result.
Go Program
package main
import "fmt"
func main() {
var flag bool = true
fmt.Println(flag)
fmt.Printf("%T", flag)
}Output
[1] true [1] bool
Example 7: Working with Complex64 Data Type
We can define and work with 64-bit complex data types in Go.
For example,
- Create a complex64 variable named
c64and assign it a value of 3 + 4i. - Print the value of
c64to the console. - Use the
Printffunction to determine the data type ofc64and print the result.
Go Program
package main
import "fmt"
func main() {
var c64 complex64 = 3 + 4i
fmt.Println(c64)
fmt.Printf("%T", c64)
}Output
[1] (3+4i) [1] complex64
Example 8: Working with Complex128 Data Type
We can define and work with 128-bit complex data types in Go.
For example,
- Create a complex128 variable named
c128and assign it a value of 3 + 4i. - Print the value of
c128to the console. - Use the
Printffunction to determine the data type ofc128and print the result.
Go Program
package main
import "fmt"
func main() {
var c128 complex128 = 3 + 4i
fmt.Println(c128)
fmt.Printf("%T", c128)
}Output
[1] (3+4i) [1] complex128