Go traps · Chapter 4 of 16

Strings are bytes

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Everyday traps
  • 3 tasks
  • Go
  • In the all labs plan

The first 2 chapters of Go traps are free. This one comes with the all labs plan.

Why you need this

Slicing a string at any byte is legal, so there is no panic and no error, and go vet says nothing. Every test written in English passes, because there one byte is one letter. Only a script whose letters take more than one byte shows the bug.

One more thing: a rune is not always what a reader sees as one letter. In Hindi and Kannada, vowel signs and the virama are runes of their own:

['स' 'ु' 'र' 'े' 'श']

So cutting by runes always gives valid text, but can leave a vowel sign behind. The standard library has nothing that cuts by what readers see as letters; for receipts, whole runes are what the gateway needs.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    repro.go: break a Kannada name on purpose

    Write reproduce() in repro.go, which returns a string. Call Shorten(name, max) from sms.go with a station's name in Kannada or Hindi that is longer than the receipt's limit of 12 characters (Malleshwaram Metro is ಮಲ್ಲೇಶ್ವರಂ ಮೆಟ್ರೋ), with max 12, and return what Shorten returned.

    The check runs your reproduce() twice, each time in a fresh build of the case's folder: beside the original sms.go it must return a string that is not valid UTF-8 (go run . shows it with \x escapes, like "ಮಲ್\xe0\xb2…"), and beside a fixed sms.go a valid string. So it must really call Shorten with a name whose letters take more than one byte. No real waiting and no input: reproduce() must finish within 3 seconds.

  2. Task 2

    sms.go: names in every script are cut cleanly

    Fix Shorten(name string, max int) string in sms.go so it counts and cuts characters (runes), not bytes. A name of at most max characters comes back unchanged. A longer name comes back as its first max-1 characters followed by …, so exactly max characters, always valid UTF-8: Shorten("Koramangala Mall", 12) is "Koramangala…".

    Keep Receipt, its limit of 12 and its line sms <driver>: <the message, quoted> as they are. The check uses its own names in English, Kannada and Hindi, short and long, and checks the exact result. Dropping the broken bytes is not enough: the name must keep max characters.

  3. Task 3

    sms_test.go: a test that would have caught it

    Write a Go test file, sms_test.go, that would have caught this bug before receipts in Kannada went out. The check runs your file three times, each in a fresh build: beside the original sms.go, where at least one test must fail; beside the fixed sms.go, where every test must pass; and beside a wrong fix that drops the broken bytes (valid UTF-8, but still cut by bytes), where at least one test must fail.

    So test Shorten with a long name in Kannada or Hindi and check the exact result, or that it is valid UTF-8 and has exactly max characters; and check that a short name in Kannada comes back unchanged. Each test must finish within a second or two (the whole file is stopped after 8 seconds).

What you will learn

  • repro.go: break a Kannada name on purpose
  • sms.go: names in every script are cut cleanly
  • sms_test.go: a test that would have caught it

More in Everyday traps

Build "Strings are bytes" yourself

BijliGo runs its EV chargers on Go, and Joseph, its senior engineer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Go traps are free. This one comes with the all labs plan.