liveBook · Manning

Strconv

As explained in Learn Go with Pocket-Sized Projects

strconv is Go's standard-library package for converting strings to and from basic numeric and quoted representations.

Use strconv when Go code must move between strings and numeric values. Itoa converts an integer to text for dynamically building a decimal format, while Atoi parses an HTML form value into an integer and returns an error for invalid input. section 6.4.3 section 11.4.2

Formatting a decimal amount

A Decimal String method builds a format such as "%d.%02d" by combining a fixed template with strconv.Itoa(int(d.precision)). It then formats integer and fractional subunits with fmt.Sprintf, preserving trailing zeroes such as 10.50. section 6.4.3

Parsing an HTML form

A request handler reads habitFrequency with r.FormValue and passes it to strconv.Atoi. The returned error becomes the safety check before calling the backend. section 11.4.2

Listing 6.30 decimal.go: Implementing the Decimal Stringer
// String implements stringer and returns the Decimal formatted as
// digits and optionally a decimal point followed by digits.
func (d *Decimal) String() string {
    // Quick-win, no need to do maths.
    if d.precision == 0 {
        return fmt.Sprintf("%d", d.subunits) #1
    }

    centsPerUnit := pow10(d.precision) #2
    frac := d.subunits % centsPerUnit
    integer := d.subunits / centsPerUnit

    decimalFormat := "%d.%0" + #3
        strconv.Itoa(int(d.precision)) + "d"  #3
    return fmt.Sprintf(decimalFormat, integer, frac)

}

Inspect the complete Decimal.String implementation, including the zero-precision branch, subunit arithmetic, dynamic format construction, and final output. section 6.4.3

Listing 11.34 create.go: Parsing form values
// create takes a JSON request and creates a Habit from it,
// then redirects to index.
func (s *Server) create(w http.ResponseWriter, r *http.Request) {
    habitName := r.FormValue("habitName") #1
    weeklyFreq, err := strconv.Atoi(
        r.FormValue("habitFrequency")) #2
    if err != nil {...}

    // call client

    // redirect to index
    http.Redirect(w, r, "/", http.StatusSeeOther) #3
}

Inspect the complete form handler fragment, including FormValue access, Atoi error capture, and redirect flow. section 11.4.2

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Demonstrates Itoa and Atoi inside working implementations, including dynamic formatting and conversion-error handling.
Examples
Uses currency amounts such as 10.50 and habit-frequency form fields as concrete conversion examples.
What it assumes
Assumes familiarity with Go methods, HTTP requests, formatted output, and basic error handling.
What you can do after
Apply it via Itoa and Atoi to construct numeric formats and parse integer-valued HTTP form inputs.
How it connects
Connects numeric conversion with Stringer implementations, decimal formatting, HTTP forms, and input validation.

Also explained in

Compare how 2 books explain strconv →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Go with Pocket-Sized Projects. The section links point to the parts of the book it draws on.