Skip to main content
eLearner.app
Module 8 · Lesson 2 of 537/50 in the course~12 min
Module lessons (2/5)

`strings` and `strconv`

strings and strconv are two packages you will use constantly. The first manipulates strings (split, trim, replace, search); the second converts between strings and basic types (int, float, bool).

strings: the functions you reach for every day

Go
import "strings"

strings.ToUpper("ciao")              // "CIAO"
strings.ToLower("CIAO")              // "ciao"
strings.Contains("ciao mondo", "mo") // true
strings.HasPrefix("foo.txt", "foo")  // true
strings.HasSuffix("foo.txt", ".txt") // true
strings.Index("ciao", "a")           // 2 (or -1 if absent)
strings.Count("banana", "a")         // 3

strings.Split("a,b,c", ",")               // []string{"a","b","c"}
strings.SplitN("a,b,c,d", ",", 2)         // []string{"a","b,c,d"}
strings.Join([]string{"a","b"}, "-")     // "a-b"

strings.TrimSpace("  ciao\n")             // "ciao"
strings.Trim("__ciao__", "_")             // "ciao"
strings.ReplaceAll("aaa", "a", "b")       // "bbb"
strings.Replace("aaa", "a", "b", 2)       // "bba"  (limit 2)

There are also EqualFold variants for case-insensitive comparisons without allocating, and Cut(s, sep) (before, after, found) for one-line parsing.

Go
key, val, ok := strings.Cut("name=Ada", "=")
// "name", "Ada", true

strings.Builder to concatenate in a loop

Concatenation s = s + "x" in a loop allocates on every iteration (strings are immutable). For text built in multiple steps use strings.Builder:

Go
var b strings.Builder
for i := 0; i < 1000; i++ {
    b.WriteString("x")
}
out := b.String()

Amortized allocation, no useless copies.

strconv: string ↔ number / bool conversions

Go
import "strconv"

n, err := strconv.Atoi("42")     // int   from decimal
s := strconv.Itoa(42)             // "42"
b, err := strconv.ParseBool("true")

// Generic versions with base and optional bit-size
i64, err := strconv.ParseInt("ff", 16, 64) // 255
u64, err := strconv.ParseUint("7", 10, 8)  // 7
f, err := strconv.ParseFloat("3.14", 64)   // 3.14

// Number → string
strconv.FormatInt(255, 16)                  // "ff"
strconv.FormatFloat(3.14, 'f', 2, 64)       // "3.14"
strconv.FormatBool(true)                    // "true"

Atoi/Itoa are ergonomic shortcuts; for fine control over base and size use ParseInt/FormatInt.

Handling parsing errors

Atoi returns (int, error). The error is almost always *strconv.NumError, and you can compare it via errors.Is against the sentinels strconv.ErrSyntax or strconv.ErrRange:

Go
n, err := strconv.Atoi(input)
switch {
case errors.Is(err, strconv.ErrSyntax):
    return fmt.Errorf("input non numerico: %q", input)
case errors.Is(err, strconv.ErrRange):
    return fmt.Errorf("numero fuori range: %q", input)
case err != nil:
    return err
}
_ = n

Exercises

Exercise#go.m8.l2.e1
Attempts: 0Loading…

Split the string 'a,b,c' on the comma and print the resulting slice.

Loading editor…

Solution available after 3 attempts

Exercise#go.m8.l2.e2
Attempts: 0Loading…

Convert the string '42' to int with strconv.Atoi, handle the error and print the number.

Loading editor…

Solution available after 3 attempts

Quiz#go.m8.l2.e3
Ready

What is the idiomatic way to convert the integer 42 into the string "42"?

Go
s := ???(42)
// vogliamo s == "42"
Answer options