1. Cheat Sheet for Strings

The strings package (what it’s for)

In Go, most text manipulation lives in the strings package. If you’re doing anything with splitting text, normalizing input, or checking content, this is where you go.

You import it with:

import
import "strings"

strings.ToLower

Converts a string to lowercase.

strings.ToLower("Hello WORLD") // "hello world"

Why it matters:

  • Case-insensitive comparisons

  • Normalizing user input

  • Counting unique words regardless of casing

This is extremely common in parsing and data processing.


strings.ToUpper

Converts a string to uppercase.

strings.ToUpper("hello") // "HELLO"

Mostly used for formatting or normalization.


strings.Fields

Splits a string into words using whitespace.

Important details:

  • Multiple spaces are automatically handled

  • Tabs and newlines count as whitespace

  • This is the right tool when you mean “words”

If you’re tempted to use []rune to split text, stop—Fields is almost always what you want.


strings.Split

Splits a string using an exact delimiter.

Use this when:

  • The delimiter is known and fixed (CSV, paths, tokens)

  • You don’t want whitespace handling

Difference from Fields:

  • Split does not collapse spaces

  • Fields does


strings.Contains

Checks if one string exists inside another.

Useful for simple substring checks.


strings.Join

Joins a slice of strings into one string.

Common when rebuilding strings after processing.


strings.TrimSpace

Removes whitespace from the start and end of a string.

Use this on user input constantly.


strings.TrimPrefix / strings.TrimSuffix

Removes specific text from the beginning or end of a string.

Safer than slicing manually.


strings.Replace / strings.ReplaceAll

Replaces substrings.


len(string) (important gotcha)

triangle-exclamation

When to use []rune

Use []rune only when:

  • You care about individual characters

  • You’re doing Unicode-aware processing

Do NOT use it for word splitting.


Common patterns you’ll see in real code

Case-insensitive comparison:

Word normalization:

Input cleanup:


Common mistakes to avoid

circle-exclamation

Bottom line

  • strings.Fields = words

  • strings.ToLower = normalization

  • Split = exact delimiter

  • []rune = characters only

  • strings package covers 90% of real-world string needs in Go


chevron-rightFurther topics you can ask me to explainhashtag
  • How Go handles Unicode vs ASCII

  • Why strings are immutable in Go

  • Performance differences between string operations