Explain Go code

Paste Go and Vibe explains what each goroutine does, what travels over each channel and where every error ends up.

counts.go

Up to 1,500 characters (about 60 lines). Your code is sent to Unvibe and Google Gemini to write the explanation. Don't paste keys or passwords. Common key formats are refused.

Vibe24 lines selected

Press Explain it for the sample, or paste your own code first.

What this Go sample shows

goroutines
go func() { ... }() starts one goroutine per line, and each one writes only its own slot, counts[i]. Since Go 1.22 every loop iteration gets its own i and text, so each goroutine parses its own line. In a module that declares go 1.21, go vet reports loop variable text captured by func literal.
channels
errs carries one result per line, nil or an error. The for range raw loop receives exactly that many, so by return counts, nil every goroutine has written its number; the race detector (go build -race) finds no race. ["3", "5", "8"] gives [3 5 8].
error values
strconv.Atoi returns its error as an ordinary value next to the number, and the function checks it with err != nil. fmt.Errorf with %w wraps it, so "7 " gives parse counts: strconv.Atoi: parsing "7 ": invalid syntax and errors.Is(err, strconv.ErrSyntax) is still true.

Good to know

  • One bad line means no counts at all: the function returns nil and the first error it receives. With no lines it returns an empty slice, not nil.
  • When several lines are bad, which error you get depends on the scheduler, not on line order. In 200 calls with ["3", "x", "y"], a normal build returned the "y" error all 200 times; a build with the race detector returned "x" 91 times and "y" 109 times.
  • Atoi accepts a sign, so "+7" gives 7, but not a trailing space: "7 " is an error.

Watch out: The buffer in make(chan error, len(raw)) is what makes the early return safe. With make(chan error), the goroutines that haven't sent yet wait for a receiver that never comes: after a call with ["x", "y", "z", "4"], 2 of them were still stuck 200 ms later, and nothing reports it.

Explain Go code beside your editor

Unvibe is a free app for Mac and Windows. In the app there is no pasting: select code in any app and press Control+U (Ctrl+Alt+U on Windows).