David in Canada

 

Tech, life, and everything in between.

Day 8 — Structs and Data Modeling in Go

Updated at # Tech # Go

Overview

Today I moved from simple procedural code to structured programming using Go structs.

The focus was on modeling real-world data (students) and organizing logic around that model. This is a key step toward writing code that resembles real backend systems.


What I Built

A Student Manager CLI that:


Key Concepts

Struct Definition

type Student struct { Name string Age int Score int }

Structs allow grouping related data into a single, type-safe structure.


Slice of Structs

students := make([]Student, 0, n)

This enables managing a collection of structured records.


Struct Methods

func (s Student) isPassing() bool { return s.Score >= 60 }

Methods attach behavior to data, making code more modular.


Data Processing Functions


Key Issues Encountered

Function vs Method

Wrong:

func isPassing(s Student) bool

Correct:

func (s Student) isPassing() bool


Top Student Initialization

Unsafe:

maxScore := 0

Better:

top := students[0]


Preallocation

make([]Student, 0, n) avoids repeated memory allocation.


Key Takeaways


Reflection

Today marked a shift from writing small scripts to building structured programs.


Next Steps