Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Strings and Type Conversion  ›  Capstone Project

Type Conversion

Type Conversion 15 minGo never converts types implicitly: you write T(v) to convert between numeric types and between strings, bytes and runes, and use the strconv package to turn numbers into text and text into numbers.

Type Conversion

The user runs tally -top 3 notes.txt. That 3 arrives as text, but tally needs an int to slice the results, and later it needs text again to print "showing 3 words". Go will not guess any of these steps for you. You convert explicitly, with the right tool for each direction.

The rule

No silent conversions: T(v) for types, strconv for text.

T(v) between compatible types. Write the target type like a function:

total, lines := 17, 4
avg := float64(total) / float64(lines) // 4.25

f := 3.9
n := int(f)       // 3: truncates toward zero
m := int(-f)      // -3

big := 200
small := int8(big) // -56: wraps to fit

Numeric conversions can lose information — the fraction, or the high bits — and Go assumes you meant it. With constants the compiler checks: int8(200) written with a literal is a compile error.

Strings, bytes and runes convert with T(v) too:

b := []byte("café")   // 5 bytes
r := []rune("café")   // 4 runes
s := string(r)        // "café" again

strconv for text ↔ numbers. Parsing text can fail, so it returns an error:

top, err := strconv.Atoi("3")  // 3, nil
_, err = strconv.Atoi("three") // 0, an error
label := strconv.Itoa(3)       // "3"
p, _ := strconv.ParseFloat("4.25", 64)

When would you convert to []byte? Whenever an API wants bytes: writing to a file with os.WriteFile, hashing, or network code. Converting a string to []byte copies the data, because the string must stay immutable while the byte slice can change.

The famous trap: string(65) does not give "65". It treats 65 as a code point and gives "A". go vet warns about it, precisely because it is almost always a mistake. int("3") does not even compile.

The shortcut

Ask "is the value text, or is the target text?" If either is, it is strconv (or fmt.Sprintf). If both are numbers, or you are moving between string, []byte and []rune, it is T(v).

The -top 3 round tripStep 1 of 4
  1. Text to number

    The argument "3" is text, the target is int: strconv.Atoi("3"), and check err.

Drills

Answer fast — trust the rule.

quick drill

f := 2.99. What is int(f)?

quick drill

What does strconv.Itoa(65) return?

quick drill

What does string(rune(65)) return?

quick drill

n, err := strconv.Atoi("12a"). What do you get?

quick drill

x := 300. What is uint8(x)?

Remember

  • No silent conversions: T(v) for types, strconv for text.
  • Shortcut: text on either side → strconv; numbers or string/bytes/runes → T(v).
  • Trap: string(65) is "A", not "65"; float → int truncates.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainReading a limit from the user

tally accepts -top 3 on the command line and later prints "showing 3 words". Explain how you turn the argument "3" into an int, how you turn the int back into text, and why int("3") and string(3) are both wrong.