DEV Community

Cover image for Strings vs Slices: a Love Triangle with Characters 💔🧵
Benjamin Koimett
Benjamin Koimett

Posted on

Strings vs Slices: a Love Triangle with Characters 💔🧵

Every developer eventually stands at this crossroads:

“Do I treat this string like a civilized adult…
or do I slice it into little byte goblins and hope for the best?”

If you’ve ever printed "🙂" and got four mysterious numbers instead, congratulations—you’ve met UTF-8 in a dark alley.

Let’s break it down. No gaslighting. No sugar. Just facts, vibes, and a little brain rot.


Option 1: Treating strings as bytes

(a.k.a. “It works on ASCII, ship it”)

In languages like Go, a string is basically:

“Here’s a read-only bag of bytes. Do not ask questions.”

Pros

🚀 Fast. Like ‘blink and you missed it’ fast

🧠 Simple mental model

📦 No extra memory allocations

Perfect for:

  • Protocols
  • File formats
  • ASCII-only data
  • When performance is king and Unicode is a rumor

Cons

  • 💥 UTF-8 will humble you
  • Slicing can split characters in half
  • (congrats, you’ve invented emotional damage)
  • Counting characters? ❌
  • You’re counting bytes. Very different beasts.

s := "Go🙂"
fmt.Println(len(s)) // 6 (bro what?)

This is the programming equivalent of:

“I asked for vibes and got taxes.”

Option 2: Converting to rune slices

(a.k.a. “I respect human languages”)

Enter: []rune.

This says:

“I want characters, not byte soup.”

Pros

🌍 Unicode-safe

🧩 One rune = one character (mostly, calm down linguists)

✂️ Safe slicing, reversing, indexing

You won’t accidentally bisect an emoji like a psycho

r := []rune("Go🙂")
fmt.Println(len(r)) // 3 (nature is healing)
Enter fullscreen mode Exit fullscreen mode

Cons

🐢 Slower (conversion costs)

🧠 More memory

Overkill if you know you’re dealing with ASCII

This is the “touch grass” option. Mature. Responsible. Slightly heavier.


So… which one is best? 🤔

Here’s the cold truth:

Neither is “best.” Context is king.

  • Use raw strings / bytes when:
  • You’re doing low-level work
  • Performance matters more than feelings
  • Input is guaranteed ASCII
  • You’re parsing, not presenting

Use rune slices when:

  • You’re manipulating text for humans
  • You care about correctness
  • Emojis, accents, or non-English text exist
  • You don’t want bug reports titled
  • “App deletes half my name 😭”

The Big Brain Take 🧠✨

Default to strings.
Switch to []rune the moment you manipulate characters.
Switch back when you’re done.

Yes, converting back and forth feels annoying.

So does debugging Unicode bugs at 2 a.m.
Pick your poison.


Final Thought (read in a dramatic narrator voice)

Bytes are fast.
Runes are safe.
Unicode is inevitable.

Choose wisely, or the emojis will unionize.

If this saved you from slicing an emoji in half, smash that like button, sacrifice a semicolon, and may your strings always be valid UTF-8.

Stay hydrated. Stay compiled. 🫡

Top comments (0)