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,

  1. Create an integer variable named x and assign it an integer value of 42.
  2. Print the value of x to the console.
  3. Use the Printf function to determine the data type of x and 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,

  1. Create an unsigned integer variable named y and assign it an integer value of 42.
  2. Print the value of y to the console.
  3. Use the Printf function to determine the data type of y and 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,

  1. Create a float32 variable named f32 and assign it a floating-point value of 42.5.
  2. Print the value of f32 to the console.
  3. Use the Printf function to determine the data type of f32 and 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,

  1. Create a float64 variable named f64 and assign it a floating-point value of 42.5.
  2. Print the value of f64 to the console.
  3. Use the Printf function to determine the data type of f64 and 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,

  1. Create a string variable named s and assign it a string value of "Hello, Go!".
  2. Print the value of s to the console.
  3. Use the Printf function to determine the data type of s and 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,

  1. Create a boolean variable named flag and assign it a value of true.
  2. Print the value of flag to the console.
  3. Use the Printf function to determine the data type of flag and 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,

  1. Create a complex64 variable named c64 and assign it a value of 3 + 4i.
  2. Print the value of c64 to the console.
  3. Use the Printf function to determine the data type of c64 and 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,

  1. Create a complex128 variable named c128 and assign it a value of 3 + 4i.
  2. Print the value of c128 to the console.
  3. Use the Printf function to determine the data type of c128 and 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