DEV Community

Ganesh Kumar
Ganesh Kumar

Posted on • Edited on

Go Programming Language: Everything You Need To Know About It (Part 6)

#go

Hello, I'm Ganesh. I'm working on FreeDevTools online, currently building a single platform for all development tools, cheat codes, and TL;DRs β€” a free, open-source hub where developers can quickly find and use tools without the hassle of searching the internet.

In my previous post, we learned about function calling and variable declaration in Go.

Now, we will learn about data types in Go.

Data Types

Go has 4 Major Data Types:

  1. Basic Data Types
  2. Aggregate Data Types
  3. Reference Data Types
  4. Interface Data Types

1. Basic Data Types

We have numbers, strings, and booleans.

bool

string

int  int8  int16  int32  int64
uint uint8 uint16 uint32 uint64 uintptr

byte // alias for uint8

rune // alias for int32
     // represents a Unicode code point

float32 float64
Enter fullscreen mode Exit fullscreen mode

int is 32 or 64 bit depending on the architecture.

uint is 32 or 64 bit depending on the architecture.

byte is an alias for uint8.

rune is an alias for int32.

float32 is 32 bit.

float64 is 64 bit.

package main

import (
    "fmt"
    "math/cmplx"
)

var (
    ToBe   bool       = false
    MaxInt uint64     = 1<<64 - 1
    z      complex128 = cmplx.Sqrt(-5 + 12i)
)

func main() {
    fmt.Printf("Type: %T Value: %v\n", ToBe, ToBe)
    fmt.Printf("Type: %T Value: %v\n", MaxInt, MaxInt)
    fmt.Printf("Type: %T Value: %v\n", z, z)
}

Enter fullscreen mode Exit fullscreen mode
Type: bool Value: false
Type: uint64 Value: 18446744073709551615
Type: complex128 Value: (2+3i)

Program exited.

Enter fullscreen mode Exit fullscreen mode

We can see here that %T shows the data type of the initialized variable.

2. Aggregate Data Types

These are collections of similar data types (homogeneous) or different data types (heterogeneous).

We can store different types of data in these (specifically in structs).

Examples include Arrays and Structures.

package main

import "fmt"

func main() {
    // Array Example
    var arr [3]int
    arr[0] = 10
    arr[1] = 20
    arr[2] = 30
    fmt.Printf("Array Type: %T Value: %v\n", arr, arr)

    // Structure Example
    type Person struct {
        Name string
        Age  int
    }
    p := Person{Name: "Alice", Age: 25}
    fmt.Printf("Struct Type: %T Value: %+v\n", p, p)
}

Enter fullscreen mode Exit fullscreen mode
Array Type: [3]int Value: [10 20 30]
Struct Type: main.Person Value: {Name:Alice Age:25}

Enter fullscreen mode Exit fullscreen mode

3. Reference Data Types

These include slices, maps, pointers, channels, and functions.

They are dynamic in nature.

They don't refer to the data directly but contain a reference to the underlying data structure.

package main

import "fmt"

func main() {
    // Slice Example
    slice := []int{1, 2, 3}
    fmt.Printf("Slice Type: %T Value: %v\n", slice, slice)

    // Map Example
    m := map[string]int{"one": 1, "two": 2}
    fmt.Printf("Map Type: %T Value: %v\n", m, m)

    // Pointer Example
    x := 10
    ptr := &x
    fmt.Printf("Pointer Type: %T Value: %v (Dereferenced: %v)\n", ptr, ptr, *ptr)
}

Enter fullscreen mode Exit fullscreen mode
Slice Type: []int Value: [1 2 3]
Map Type: map[string]int Value: map[one:1 two:2]
Pointer Type: *int Value: 0xc000018030 (Dereferenced: 10)

Enter fullscreen mode Exit fullscreen mode

Here, we can see:

  • The slice [1 2 3] holds a sequence of integers.
  • The map holds key-value pairs (e.g., "one" maps to 1).
  • The pointer holds the memory address (0xc0...), and dereferencing it (*ptr) gives the actual value 10.

4. Interface Data Types

An interface type specifies a method set called its interface.

A variable of an interface type can store a value of any type with a method set that is a superset of the interface. Such a type is said to implement the interface.

package main

import (
    "fmt"
    "math"
)

type Shape interface {
    Area() float64
}

type Circle struct {
    Radius float64
}

func (c Circle) Area() float64 {
    return math.Pi * c.Radius * c.Radius
}

func main() {
    var s Shape
    s = Circle{Radius: 5}
    fmt.Printf("Interface Type: %T Value: %+v Area: %.2f\n", s, s, s.Area())
}

Enter fullscreen mode Exit fullscreen mode
Interface Type: main.Circle Value: {Radius:5} Area: 78.54

Enter fullscreen mode Exit fullscreen mode

Here, we can see the Shape interface is satisfied by the Circle struct because Circle has an Area() method. even though s is defined as a Shape, it holds a Circle value, and calling Area() executes the Circle's implementation.

Conclusion

We have learned about data types in Go.


FreeDevTools

I’ve been building for FreeDevTools.

A collection of UI/UX-focused tools crafted to simplify workflows, save time, and reduce friction when searching for tools and materials.

Any feedback or contributions are welcome!

It’s online, open-source, and ready for anyone to use.

πŸ‘‰ Check it out: FreeDevTools

⭐ Star it on GitHub: freedevtools

Top comments (0)