ProgramGuru

String Conversion

Start with String Conversion

Converting between strings and numbers requires deliberate parsing or formatting. The strconv package provides the standard functions for these tasks.

What you’ll learn

  • Explain the main purpose of string conversion.
  • Read and run the complete Go example in this lesson.
  • Apply the concept and recognize common mistakes.

Understand the idea

Use strconv.Atoi to parse a decimal integer from text. It returns both an integer and an error; check that error before using the result. Use strconv.Itoa to format an integer into decimal text.

For other bases and numeric types, ParseInt, ParseFloat, FormatInt and similar helpers offer more control. Converting an integer directly with string(n) produces the UTF-8 representation of a Unicode code point, not its decimal digits.

Visualize how it works

Follow this diagram before executing the example. Check where a value or decision changes what runs next.

Visual explanation for string conversionParsing and formatting go in opposite directions.Input "42"Step 1Atoi -> int 42Step 2Math -> int 43Step 3Itoa -> text "43"Step 4Parsing and formatting go in opposite directions.
Parsing and formatting go in opposite directions.

Run a complete Go example

This example is a complete program. Save it as main.go or paste it into the Go Playground.

Go code

package main

import (
	"fmt"
	"strconv"
)

func main() {
	number, err := strconv.Atoi("42")
	if err != nil {
		fmt.Println("Invalid number")
		return
	}
	fmt.Println("Next:", number+1)
	text := strconv.Itoa(123)
	fmt.Println("Text:", text+"4")
	_, bad := strconv.Atoi("four")
	fmt.Println("Invalid input:", bad != nil)
}

Output

Next: 43
Text: 1234
Invalid input: true

Trace what the program does

Atoi parses "42" into integer 42, so addition produces 43. Itoa converts 123 to the string "123"; concatenating the character 4 produces "1234". Parsing "four" fails, so the final boolean is true.

Avoid common mistakes

  • Do not use string(42) when you mean the digits "42"; use strconv.Itoa instead.
  • Always check a parse error before trusting the resulting numeric value.

Try it yourself

Parse the text "125", multiply its numeric value by two, then format the answer back into a string.

Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.

Check your understanding

Which function formats int 42 as the text "42"?
Choose one answer. You can try again.

Answer and explanation

strconv.Itoa(42)

Correct. Atoi parses "42" into integer 42, so addition produces 43.

What you should remember

You have practiced String Conversion in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.

Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Maps →