Understanding Data Types in Go (GoLang) with Examples

Tejaksha K
2 min readMay 16, 2024

--

Go, also known as Golang, is a statically typed, compiled programming language designed for simplicity and efficiency. Understanding the various data types available in Go is essential for writing efficient and maintainable code. In this article, we’ll explore the different data types in Go along with examples.

Primitive Types

Integer :

Integers represent whole numbers. In Go, integers can be signed or unsigned and vary in size.

var age int = 30

Floating-Point :

Floating-point numbers represent decimal numbers.

var temperature float64 = 98.6

Boolean :

Boolean values represent true or false.

var isRaining bool = true

Rune (Character) :

In Go, characters are represented as runes, which are encoded as UTF-8 code points.

var firstLetter rune = 'A'

String :

Strings represent a sequence of characters.

var message string = "Hello, world!"

Composite Types

Array

Arrays represent a fixed-size collection of elements of the same type.

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

Slice

Slices represent a dynamic-size collection of elements.

var colors []string = []string{"green", "blue", "black"}

Map

Maps represent a collection of key-value pairs.

var ages map[string]int = map[string]int{"Alice" : 30, "Bob" : 35}

Struct

Structs represent a composite data structure with named fields.

type Person struct {
Name string,
Age int
}

var person1 Person = Person{Name : "Alice", Age: 30 }

Functional Types

Function

Functions represent a block of code that can be called and executed.

func add(a,b int) int {
return a + b
}

Interface Types

Interface

Interfaces define a set of methods that a type must implement.

type Shape interface {
Area() float64
}

type Rectangle struct {
Width, Height float64
}

func (r Rectangle) Area() float64 {
return r.Width * r.Height
}

User-Defined Types

Struct

Structs represent a composite data structure with named fields.

type Person struct {
Name string
Age int
}

var person1 Person = Person{Name: "Alice", Age: 30}Enumeration (Enum)

Enumeration (Enum)

Go doesn’t have built-in enums, but you can create a similar effect using constants.

type Status int

const (
Pending Status = iota
Approved
Rejected
)

var applicationStatus Status = Pending

Understanding these data types in Go will enable you to write clear and efficient code for a wide range of applications.

Happy Coding 🚀😻

--

--

Tejaksha K
Tejaksha K

Written by Tejaksha K

I'm a Full Stack Developer & Cloud Expert with experience in Google Cloud Platform & AWS.

No responses yet