Adding to a Set
Part of the Logic & Flow section of Coddy's GO journey — lesson 33 of 68.
Now that you have a set created, the next step is learning how to add elements to it. Since a Go set is actually a map underneath, adding an element to a set is identical to adding a key to a map.
To add an element to your set, you simply assign the empty struct value to a new key:
colors["yellow"] = struct{}{}The struct{}{} syntax creates an empty struct literal. The first struct{} specifies the type, and the second {} initializes it. This is the standard way to add elements to a set in Go.
The beauty of this approach is that if you try to add the same element again, it simply overwrites the existing entry, maintaining the set's uniqueness property.
Note that struct{}{} is the only valid syntax for this assignment — writing just {} without the type name causes a compilation error: missing type in composite literal. The type must always be explicitly provided when assigning a value to a map key outside of a map literal.
Adding elements to a set is fast and straightforward, leveraging Go's efficient map implementation while giving you true set behavior for tracking unique items.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Challenge
EasyBuild a skill tracking system that manages a developer's technical abilities using the Go set idiom. This challenge demonstrates how to add new skills to a set while preventing duplicates and tracking the learning progress.
You will receive two inputs:
- A string representing the expected number of skills (e.g.,
"5") - this is for reference only - A string containing skills separated by commas (e.g.,
"Go,Docker,Kubernetes,Go,React,Docker,Python")
Your task is to:
- Create a set using the Go idiom
map[string]struct{}to store unique technical skills - Initialize the set with three starter skills:
"Programming","Problem Solving", and"Communication" - Parse the input string by splitting it on commas to get individual skill names
- Process ALL skills from the comma-separated string (iterate through the entire slice, not using the count parameter)
- For each skill from the input, add it to your set using
struct{}{}as the value - Display the processing results by printing each skill as it's processed:
- If the skill is new to the set:
"Learning new skill: [skill]" - If the skill already exists in the set:
"Already mastered: [skill]"
- If the skill is new to the set:
- After processing all skills, display a progress summary:
"Skills processed: [total_processed]"- use the length of the skills slice"New skills learned: [new_skills_count]""Total skills mastered: [total_unique_skills]"
- Finally, list all skills in the developer's skill set:
- Header:
"Complete skill set:" - Each skill on a separate line:
"✓ [skill]"
- Header:
Important: Use range to iterate over all skills in the slice, not the count parameter. The count is provided for reference but should not control your loop. Use the strings package to split the input string. To check if a skill already exists before adding it, use the comma ok idiom: _, exists := skillSet[skill]. This challenge demonstrates how adding elements to a Go set automatically handles uniqueness while providing fast lookup performance for membership testing.
Cheat sheet
To add elements to a Go set (implemented as map[string]struct{}), assign an empty struct to a new key:
colors["yellow"] = struct{}{}The struct{}{} syntax creates an empty struct literal. The first struct{} specifies the type, and the second {} initializes it. This is the standard way to add elements to a set in Go.
Try it yourself
package main
import (
"fmt"
"sort"
"strconv"
"strings"
)
func main() {
// Read input
var countStr string
var skillsStr string
fmt.Scanln(&countStr)
fmt.Scanln(&skillsStr)
// Convert count string to integer (for reference only - not used in loop)
_, _ = strconv.Atoi(countStr)
// Initialize skill set with starter skills
skillSet := map[string]struct{}{
"Programming": struct{}{},
"Problem Solving": struct{}{},
"Communication": struct{}{},
}
// Split skills string into individual skills
skills := strings.Split(skillsStr, ",")
// TODO: Write your code below
// Process each skill using range to iterate over ALL skills in the slice
// Use comma ok idiom: _, exists := skillSet[skill]
// Add new skills using: skillSet[skill] = struct{}{}
// Display processing results for each skill
// Calculate and display progress summary using len(skills)
// List all skills in the complete skill set (consider sorting for consistent output)
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Advanced Control Flow
Switch with `fallthrough`Breaking from Nested LoopsContinuing a Specific LoopThe `goto` StatementRecap - Advanced Loop Control4Project: Simple Task List
Project SetupAdding a Task2Structs and Methods
Defining Methods on StructsValue ReceiversPointer ReceiversChoosing ReceiversMethods vs FunctionsRecap - Struct Behavior5Maps In-Depth
Maps of StructsPointers as Map ValuesTesting for Nil MapsComparing MapsRecap - Word Frequency Counter3Interfaces (The Basics)
What is an Interface?Defining an InterfaceImplementing an InterfaceUsing Interface TypesEmpty InterfaceType AssertionsType SwitchRecap - Shapes and Behaviors6Idiomatic Go: Sets
The Set Idiom in GoCreating a SetAdding to a SetChecking for MembershipRemoving from a SetIterating Over a SetRecap - Unique Usernames