GoLang - Data Types

2 minute read

In this tutorial, you will learn about Data type in Go Programming with the help of examples.

Data types in Go programming language are used to define the type of value that a variable can hold. There are several built-in data types available in Go, which can be categorized into four categories - Basic, Composite, Reference, and Interface types.

Basic Data Types

The basic data types in Go include:

Boolean

It represents a true or false value. The type is bool and the value can be either true or false.

Example:

var isTrue bool = true
var isFalse bool = false

Numeric Types

Numeric types in Go include integers and floating-point numbers. Integers can be of different sizes, including 8-bit, 16-bit, 32-bit, and 64-bit integers. Floating-point numbers can be of two types, float32 and float64.

var i int = 10
var j int64 = 100000
var f float32 = 1.2345
var d float64 = 123.456789

String

A string is a sequence of characters enclosed in double quotes. The type is string.

Example:

var message string = "Hello, World!"

Composite Data Types

Composite data types in Go are used to represent a collection of values. They include:

Arrays

Arrays are a collection of elements of the same type, stored in a contiguous memory location. The size of the array is fixed at the time of declaration.

Example:

var numbers [5]int = [5]int{1, 2, 3, 4, 5}

Slices

Slices are similar to arrays, but their size is not fixed. They are a reference to an underlying array and can be resized dynamically.

Example:

var numbers []int = []int{1, 2, 3, 4, 5}

Maps

Maps are used to store key-value pairs. The type is map[keyType]valueType, where keyType and valueType are the types of the key and value respectively.

Example:

var details map[string]string = map[string]string{"name": "John", "age": "30"}

Reference Data Types

Reference data types in Go are used to represent a reference to a memory location. They include:

Pointers

A pointer is a variable that stores the memory address of another variable. The type of a pointer is *T, where T is the type of the variable being pointed to.

Example:

var x int = 10
var ptr *int = &x

Interface Types

Interface types in Go are used to define a set of methods that a type must implement. They include:

Interfaces

An interface is a collection of method signatures. A type implements an interface by implementing all the methods defined in the interface.

Example:

type Shape interface {
    Area() float64
    Perimeter() float64
}

type Rectangle struct {
    width  float64
    height float64
}

func (r Rectangle) Area() float64 {
    return r.width * r.height
}

func (r Rectangle) Perimeter() float64 {
    return 2 * (r.width + r.height)
}

func main() {
    var shape Shape = Rectangle{10, 20}
    fmt.Println(shape.Area()) // Output: 200
    fmt.Println(shape.Perimeter()) // Output: 60
}

In conclusion, understanding data types is crucial in Go programming, as it helps in writing efficient and correct code.

Updated: