Menu

Post image 1
Post image 2
Post image 3
1 / 3
0

Go's range-over-func: 4 Footguns the Compiler Won't Warn You About

DEV Community·Gabriel Anhaia·about 1 month ago
#aewUDahE
#footgun#go#programming#yield#func#return
Reading 0:00
15s threshold

Book: The Complete Guide to Go Programming Also by me: Thinking in Go (2-book series) — Complete Guide to Go Programming + Hexagonal Architecture in Go My project: Hermes IDE | GitHub — an IDE for developers who ship with Claude Code and other AI coding tools Me: xgabriel.com | GitHub Range-over-func shipped in Go 1.23. The standard library has been quietly ranging over iter.Seq and iter.Seq2 ever since, and most teams have absorbed the basics. You write a function that takes a yield func(T) bool , you call yield in a loop, you return when yield says stop. Two screens of documentation and a curious afternoon. The part that is not fine is what happens when you push past the textbook example. The compiler will accept code that looks reasonable to a human and then panic at runtime with "range function continued iteration after function for loop body returned false" . There is no vet rule that catches it. The type system has no concept of yield's lifetime.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More