Go (Golang) Data Types & Data Structures — Complete Guide

 

Go (Golang) Data Types & Data Structures — Complete Guide

Introduction

Go is a statically typed, compiled language designed for:

  • Simplicity

  • Performance

  • Concurrency

  • Predictable memory behavior

In Go:

  • Every variable has a fixed type

  • Types are checked at compile time

  • Zero values are automatically assigned

var x int // default value = 0 var s string // default value = ""

Categories of Go Data Types

Go data types can be grouped into:

  1. Basic Types

  2. Composite Types

  3. Reference Types

  4. Interface Types


1️⃣ Basic Data Types

a) Integer Types

var a int = 10 var b int64 = 100 var c uint = 20

Common integer types:

  • int, int8, int16, int32, int64

  • uint, uint8(byte), uint16, uint32, uint64

fmt.Println(a + 5)

b) Floating Point Types

var price float64 = 99.99 var temp float32 = -10.5
fmt.Println(price * 2)

c) Boolean

var isActive bool = true
if isActive { fmt.Println("Active user") }

d) String

var name string = "Vinod"

Strings are immutable in Go.

fmt.Println(name) fmt.Println(len(name))

Iterating characters:

for i, ch := range name { fmt.Println(i, string(ch)) }

2️⃣ Array (Fixed Size)

Arrays have fixed length.

var arr [3]int = [3]int{1, 2, 3}

Access:

fmt.Println(arr[0])

⚠️ Arrays are rarely used directly in Go.


3️⃣ Slice (MOST IMPORTANT)

Slices are dynamic, flexible views over arrays.

Create a slice

nums := []int{1, 2, 3}

Append

nums = append(nums, 4)

Access

fmt.Println(nums[0])

Update

nums[1] = 20

Iterate

for i, v := range nums { fmt.Println(i, v) }

Slice internals (Interview Gold)

A slice has:

pointer → array length capacity
fmt.Println(len(nums), cap(nums))

4️⃣ Map (Key–Value Store)

Maps store unordered key–value pairs.

Create map

user := map[string]int{ "age": 35, "score": 100, }

Add / Update

user["age"] = 36

Retrieve

age := user["age"]

Check existence

val, ok := user["city"] if !ok { fmt.Println("Key not found") }

Delete

delete(user, "score")

5️⃣ Struct (Custom Data Type)

Structs group related data.

type User struct { Name string Age int }

Create and use:

u := User{Name: "Vinod", Age: 35} fmt.Println(u.Name)

Pointer to struct:

pu := &u pu.Age = 36

6️⃣ List (container/list – Doubly Linked List)

Go provides a linked list via container/list.

import "container/list" l := list.New()

Add elements

l.PushBack(10) l.PushBack(20) l.PushFront(5)

Iterate

for e := l.Front(); e != nil; e = e.Next() { fmt.Println(e.Value) }

Use cases:

  • Frequent insert/delete

  • No random access


7️⃣ Set (Using map)

Go has no built-in set, but maps are used.

set := make(map[int]bool)

Add

set[1] = true

Check

if set[1] { fmt.Println("Exists") }

Delete

delete(set, 1)

8️⃣ Pointer Types

Pointers store memory addresses.

x := 10 p := &x
fmt.Println(*p) // dereference

Used for:

  • Performance

  • Mutability

  • Struct updates

  • Large data passing


9️⃣ Interface (Polymorphism)

Interfaces define behavior.

type Speaker interface { Speak() string }

Implement interface:

type Person struct { Name string } func (p Person) Speak() string { return "Hello " + p.Name }

Use:

var s Speaker = Person{Name: "Vinod"} fmt.Println(s.Speak())

🔟 Zero Values (Very Important)

Go automatically assigns zero values.

TypeZero Value
int0
float0.0
boolfalse
string""
slicenil
mapnil
pointernil

1️⃣1️⃣ Mutable vs Immutable

TypeMutable
int, float, string
slice
map
struct
array❌ (value copy)

1️⃣2️⃣ Common Real-World Examples

List of users

users := []User{ {Name: "A", Age: 30}, {Name: "B", Age: 25}, }

Lookup by ID

usersMap := map[int]User{ 1: {Name: "A"}, }

Unique IDs

ids := make(map[int]struct{}) ids[100] = struct{}{}

1️⃣3️⃣ Summary Table

TypeBest Use
int / floatNumbers
stringText
arrayFixed size
sliceDynamic lists
mapFast lookup
structCustom objects
listFrequent inserts
interfacePolymorphism
pointerPerformance

1️⃣4️⃣ Interview One-Line Summary ⭐

Go provides strong, static data types with powerful composite structures like slices, maps, structs, and interfaces, enabling efficient, predictable, and concurrent-safe programs.

No comments:

Post a Comment

Confusion Matrix + Precision/Recall (Super Simple, With Examples)

  Confusion Matrix + Precision/Recall (Super Simple, With Examples) 1) Binary Classification Setup Binary classification means the model p...

Featured Posts