Recap - Custom Data Structures
Part of the Fundamentals section of Coddy's GO journey — lesson 101 of 109.
Challenge
EasyLet's create a simple custom data structure to recap what we've learned about structs in Go.
In this challenge, you'll create a basic Counter data structure that can:
- Track a count value
- Increment the count
- Decrement the count
- Reset the count to zero
Complete the code by implementing the missing methods for the Counter struct.
Try it yourself
package main
import (
"fmt"
)
// Counter is a simple counter data structure
type Counter struct {
value int
name string
}
// NewCounter creates a new counter with a given name
func NewCounter(name string) Counter {
return Counter{
value: 0,
name: name,
}
}
// Increment increases the counter by 1
func (c *Counter) Increment() {
// Add code to increment the counter's value
}
// Decrement decreases the counter by 1 (but not below 0)
func (c *Counter) Decrement() {
// Add code to decrement the counter's value
// Make sure the value doesn't go below 0
}
// Reset sets the counter back to 0
func (c *Counter) Reset() {
// Add code to reset the counter to 0
}
// Value returns the current count
func (c Counter) Value() int {
// Add code to return the current value
return 0 // Replace this
}
// String returns a string representation of the counter
func (c Counter) String() string {
return fmt.Sprintf("%s: %d", c.name, c.value)
}
func main() {
// Create a new counter named "Visitors"
visitors := NewCounter("Visitors")
// Increment the counter a few times
visitors.Increment()
visitors.Increment()
visitors.Increment()
// Print the current count
fmt.Println(visitors)
fmt.Println("Current value:", visitors.Value())
// Decrement the counter
visitors.Decrement()
fmt.Println(visitors)
// Reset the counter
visitors.Reset()
fmt.Println(visitors)
// Test that counter doesn't go below zero
visitors.Decrement()
fmt.Println(visitors)
}All lessons in Fundamentals
4Comparison & Logical Operators
Comparison Operators - Part 1Comparison Operators - Part 2Logical AND OperatorLogical OR OperatorLogical NOT OperatorOperator Precedence BasicsRecap - Making Comparisons7Control Flow: Loops
What The `for` Loop ExplainedFor Loop - BasicFor Loop - Condition OnlyThe `break` KeywordThe `continue` KeywordNested LoopsRecap - Repeating Actions10Composite Types: Arrays
Introduction to ArraysDeclaring ArraysInitializing ArraysAccessing Array ElementsArray Length with `len`Iterating Over ArraysMulti-dimensional Arrays13Composite Types: Structs
Defining Custom TypesCreating Struct InstancesAccessing Struct FieldsPointers to StructsInitializing StructsEmbedded StructsAnonymous StructsRecap - Custom Data Structures2Variables and Basic Data Types
What is a variableType Inference with `:=`Integers (int)Floating-Point NumbersBooleansStringsZero ValuesConstantsNaming ConventionsRecap - Variables and Types5Basic Input/Output
Formatted OutputFormat VerbsPrinting TypesGetting Basic User InputRecap - Input and Output8Functions
Understanding FunctionsDeclaring a FunctionCalling FunctionsFunction ParametersReturning a Single ValueReturning Multiple ValuesNamed Return ValuesFunction Scope BasicsRecap - Creating Reusable Code3Basic Operators
Arithmetic OperatorsDivision OperatorThe Modulo OperatorAssignment OperatorAugmented Assignment OperatorsIncrement and DecrementRecap - Calculations6Control Flow: Conditionals
The `if` StatementThe `else` KeywordThe `else if` KeywordVariable Shadowing in `if`Initializing VariablesThe `switch` StatementSwitch with ExpressionsSwitch without ExpressionThe `fallthrough` KeywordRecap - Making Decisions