TryBugHunt
goOtherEasy

Site names lose their first letters when the scheme is removed

BugFind the bug and fix the code

What it should do

siteName(url) removes a leading "https://" from a URL. siteName("https://shop.com") returns "shop.com".

What happens instead

siteName("https://shop.com") returns "op.com".

package main

import "strings"

func siteName(url string) string {
    return strings.TrimLeft(url, "https://")
}

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.TrimLeft does not remove a prefix. Its second argument is a set of characters, and it strips every leading character that is in that set: h, t, p, s, colon and slash. After "https://" it keeps going and eats the s and h of "shop". Sites starting with other letters, like example.org, come out fine, which makes the bug look random. strings.TrimPrefix removes the exact string once. The same trap exists with TrimRight versus TrimSuffix and Trim.

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/7666c8ba-efcf-4d15-8204-a8eb84030f7f" title="Debugging challenge: Site names lose their first letters when the scheme is removed" 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/7666c8ba-efcf-4d15-8204-a8eb84030f7f">TryBugHunt</a></p>