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

