Gointrogo1

strings and strconv

The two packages you reach for on day one of text wrangling.

Go strings are immutable UTF-8 byte sequences — strings slices, joins, and searches them, while strconv bridges text and numbers.

Strings are immutable bytes

A Go string is a read-only sequence of bytes, almost always holding UTF-8 text. You cannot mutate one in place, so every "edit" returns a new string. The strings package is the standard toolkit for that: Split and Join move between a string and a []string, Fields breaks on runs of whitespace, and Contains, ToUpper, and ReplaceAll cover the everyday search-and-transform cases.

Building strings efficiently

Because each concatenation allocates, gluing many pieces with + in a loop is wasteful. strings.Builder is the idiomatic fix: it accumulates into a growable buffer and hands you the final string once via String(). Reach for it whenever you assemble output from a loop.

Crossing the text–number boundary

The strconv package converts between strings and numeric types. Itoa turns an int into its decimal string and Atoi parses one back — Atoi returns a value and an error, so always check it. For floats, FormatFloat gives you precise control over format and precision, which fmt defaults can't always match.

Try it 5 examples

Split a CSV row, then re-join it

Gointro
0ms
[alice bob carol] count: 3alice | bob | carol
Reference · output compiled offline, not runnable in-browser

Split turns the comma-delimited string into a []string of length 3, and Join glues those parts back with a new separator — showing the round trip between a string and its slice form.