Explain Go code
Paste Go and Vibe explains what each goroutine does, what travels over each channel and where every error ends up.
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 owniandtext, so each goroutine parses its own line. In a module that declaresgo 1.21,go vetreportsloop variable text captured by func literal.- channels
errscarries one result per line,nilor an error. Thefor range rawloop receives exactly that many, so byreturn counts, nilevery goroutine has written its number; the race detector (go build -race) finds no race.["3", "5", "8"]gives[3 5 8].- error values
strconv.Atoireturns its error as an ordinary value next to the number, and the function checks it witherr != nil.fmt.Errorfwith%wwraps it, so"7 "givesparse counts: strconv.Atoi: parsing "7 ": invalid syntaxanderrors.Is(err, strconv.ErrSyntax)is stilltrue.
Good to know
- One bad line means no counts at all: the function returns
niland the first error it receives. With no lines it returns an empty slice, notnil. - 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. Atoiaccepts 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).