Menu
Coddy logo textTech

Recap - Custom Data Structures

Part of the Fundamentals section of Coddy's GO journey — lesson 101 of 109.

challenge icon

Challenge

Easy

Let'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:

  1. Track a count value
  2. Increment the count
  3. Decrement the count
  4. 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