TryBugHunt
goOtherEasy

Posts with no tags are counted as having one

BugFind the bug and fix the code

What it should do

countTags(tagList) counts the tags in a comma-separated list such as "go,web,api". An empty string means no tags. countTags("go,web,api") returns 3 and countTags("") returns 0.

What happens instead

countTags("") returns 1.

package main

import "strings"

func countTags(tagList string) int {
    return len(strings.Split(tagList, ","))
}

Test a custom input to verify an edge case (e.g. empty lists, negative numbers, duplicates).

⏱️0:00
Hints
🏆 0 hints = Flawless Solve
Think & Test Cooldown
2:00 remaining

Take a moment to read the bug symptom and trace the code with Run & Check before unlocking Hint 1.

Hint 2
Unlocks after Hint 1
Hint 3
Unlocks after Hint 2

This bug belongs to the mutation and copying bugs pattern

Slices that share one backing array, so changing a copy changes the original.

What you'll practise

An easy Go debugging exercise about mutation and copying bugs: slices that share one backing array, so changing a copy changes the original.

The checks your fix must pass

Signs of this kind of bug

Spoiler: why this bug happens(try it first; the hints get warmer one at a time)

strings.Split always returns at least one element: splitting "" gives [""], a slice with one empty string. That is consistent (no separator found means one piece, the whole input), but it means an empty input is counted as one tag. strings.Fields behaves differently and returns an empty slice for blank input, but it splits on spaces, not commas. Handle the empty string before splitting, or filter out empty pieces afterwards.

The pattern behind it: mutation and copying bugs.

More mutation and copying bugs to practise

Embed this challenge on your site

Paste this into any web page or blog post. Visitors fix the bug right there, in their browser.

<iframe src="https://trybughunt.com/embed/0381d127-bd81-4d52-88d6-cc1d609d8f00" title="Debugging challenge: Posts with no tags are counted as having one" width="100%" height="580" loading="lazy" style="border:1px solid #d4d4d8;border-radius:12px;max-width:760px"></iframe>
<p style="font-size:14px">Debugging challenge from <a href="https://trybughunt.com/challenges/0381d127-bd81-4d52-88d6-cc1d609d8f00">TryBugHunt</a></p>