Go typing practice: blocks and returns

Type a small Go function twice: once from the displayed reference and once from its block structure. Comparing your raw copy with gofmt shows which differences are formatting and which change the code you meant to write.

Language practice guide
In this guide, you’ll practice

Copy the function → compare the formatting → rebuild its blocks

01 Read the function by its blocks

For non-negative stock and demand, split returns two integers in this order: units supplied and units still needed. Read the second pair of parentheses as result types, not another argument list.

split.goFormatted reference
func split(stock, want int) (int, int) {
    if want > stock {
        return stock, want - stock
    }
    return want, 0
}

02 Compare the compact copy with gofmt

The compact example is valid Go. Compare the return commas, brace boundaries and operators before formatting. Run gofmt -d split.go in your editor on a file with a package declaration; it shows the diff without modifying the file.

split.goBefore gofmt
func split(stock,want int)(int,int){
if want>stock{return stock,want-stock}
return want,0
}
Reveal what formatting can and cannot fix

Expanded formatting comparison

func split(stock, want int) (int, int) {
    if want > stock {
        return stock, want - stock
    }
    return want, 0
}

Compare spaces around commas and operators and the guard indentation. Tabs are represented as spaces in this preview. Formatting does not fix the return order. Both input checks passed, and gofmt 1.24.4 produced this layout.

03 Practice with Go code

Start in your editor, then carry one pattern into the timed test. The article example and the test passage are separate.

  1. 01
    Copy the reference exactly

    Copy the displayed function without relying on format-on-save. Compare delimiters, indentation and line breaks before you run gofmt.

  2. 02
    Rebuild the blocks from memory

    Rebuild signature → guard → final return. Check split(3, 5) gives (3, 2) and split(5, 3) gives (3, 0).

  3. 03
    Try the block rhythm in a Go test

    Notice where you lose your place: after the parameter list, at the guard’s closing brace, or at the comma between return values.

Try the Go typing test

Look for the same typing pattern in the displayed passage.

Start Go typing test

04 Review the copy and the result

A formatting diff can expose spacing and indentation changes, but swapped return values can still compile. Check the order of the two results before judging the copy by its formatting.

In the timed test, record the language, duration, CPM, accuracy and one transition to revisit. Compare runs of similar duration and mode; different snippets can change the result.

Understand CPM and WPM for code →

05 Keep learning