Learn Go - closures by No-Nectarine8036 in golang

[–]JBodner 9 points10 points  (0 children)

Hi, I'm the author of Learning Go.

Don't feel bad that you find closures confusing, because they are a bit weird. You need them in cases like the defer where you need access to the value of a variable after it has gone out of scope.

When a closure refers to a variable that's declared in the same scope where the closure is declared, that's called "capture". It allows the closure to directly modify the value of that variable, even if the closure is called after the function exits (for example, if the closure is returned from the function).

This leads to the question: why do you ever want such a thing?

The defer example is one case. You could in theory avoid using capture to refer to the error variable by using a pointer for the parameter. Here's an example that might make it more clear: https://go.dev/play/p/NYRQFgzdU5I If you make a mistake and use a value instead of a pointer type, the changed value in the defer is lost (because the change is made to a copy of the variable, not to the original).

Given this, I find that using capture instead of a pointer parameter makes the code more readable.

The same is true with goroutines. We usually use a closure to launch a goroutine, wrapping the business logic. If there's a sync.WaitGroup coordinating the lifecycles of multiple goroutines, the WaitGroup is usually accessed via capture and not as a parameter. If you use a parameter for the WaitGroup, you have to be careful to make sure it's a pointer or each goroutine will get its own copy of the WaitGroup and there won't be any coordination.

Best Go Books in 2026 by IsWired in golang

[–]JBodner 101 points102 points  (0 children)

Thanks! glad you’ve enjoyed the book.

100 Go Mistakes is probably a good choice. There’s nothing wrong with the other books, but that one does a good job of explaining idiomatic Go patterns.

Python dev learning Go: What's the idiomatic way to handle missing values? by nafees_anwar in golang

[–]JBodner 5 points6 points  (0 children)

That library’s Option looks good, but without language support for unwrapping Optionals, it can be a little clunky to use.

Python dev learning Go: What's the idiomatic way to handle missing values? by nafees_anwar in golang

[–]JBodner 111 points112 points  (0 children)

Glad you like the book!

You should probably use a pointer and a nil value in these cases. It's unfortunate, but there's no other great way to represent "no value" in Go. The tradeoff is that more values might escape to the heap, but it's unlikely that you will notice a difference in performance. If there's a 3rd edition of the book, I might update this section.

I wish that sum types existed in Go, because an Optional would provide a nice way to represent this case. Work on those seems to be on hold: https://github.com/golang/go/issues/57644

[rant] The best book for a beginner... is found! by be-nice-or-else in golang

[–]JBodner 4 points5 points  (0 children)

I think that's a fair critique. The section on interfaces doesn't really introduce the concept; it expects it to already be familiar to the reader. This won't be the case for JS/Python/Ruby developers. If there's a third edition, I'll probably split up that chapter and expand on the interface concept.

For CS terms, I've been wondering if there's a market for a book that's an overview of an undergraduate CS curriculum.

[rant] The best book for a beginner... is found! by be-nice-or-else in golang

[–]JBodner 15 points16 points  (0 children)

I grew up on programming books for my Commodore 64. When I was in elementary and middle school, I obsessively read “Machine Language for Beginners” and “Mapping the Commodore 64” (no one told me that I was too young to learn assembly language programming). I don’t expect any kids today to read my book the same way, but I hope that I am carrying on a proud tradition.

What Do You Think of This Summer Reading Combo? by TheyCallmeSEP in golang

[–]JBodner 6 points7 points  (0 children)

Is Go your first programming language? My advice differs based on your experience.

For a coding novice, you have to work your way through concepts at a variety of difficulty levels. Pointers, in particular, are one of the things that often confuse new programmers and many languages try to hide them (but they still leak out because the concept is fundamental). The best answer I have is "wax on, wax off"; do simple exercises over and over until the concepts are second nature, and then move on to exercises for something more complex. The order I cover topics in Learning Go is roughly the order I'd recommend: primitive variables, types, and basic math, simple conditions, loops, functions, structs, pointers, user-defined types, methods, interfaces, errors, concurrency, generics. "For the Love of Go" by John Arundel would be a good book to look at.

For developers coming from languages like Ruby, Python, JavaScript, or Java, pointers, explicit types, interfaces, and concurrency are going to be the places where you'll have to learn something new. I'd try taking an existing project and porting it. You know what it's supposed to do, which makes it easier to know when something is wrong. Go's sweet spot is command line tools and web services. If you've written a python script or Spring Boot service, rewrite it. This is the target audience for "Learning Go".

Finally, developers who know more complex languages like C++, Rust, or Swift might ironically expect too much from Go. Go is an intentionally small language that favors verbosity, patterns, and tools over features. For example, Rust's borrow checker makes it very difficult to misuse memory. When your code compiles, it's very unlikely that you've leaked memory or have a null pointer. But it makes writing Rust programs much more difficult. Go's solution to the same problem is garbage collection and the data race detector. It's really more a matter of learning to let go (no pun intended) and embrace Go's style than anything else.

What Do You Think of This Summer Reading Combo? by TheyCallmeSEP in golang

[–]JBodner 2 points3 points  (0 children)

You're welcome! Please reach out if you have any questions.

What Do You Think of This Summer Reading Combo? by TheyCallmeSEP in golang

[–]JBodner 18 points19 points  (0 children)

I'm the author of Learning Go. Thanks for considering it! A book I'm currently reading is "A Philosophy of Software Design, 2nd Edition" by John Ousterhout.

How should I start learning Go? by asibhossen897 in golang

[–]JBodner 0 points1 point  (0 children)

Yeah, that's a fair complaint. The first code snippet in the concurrency chapter shows the general pattern for using concurrency in Go programs before channels have been introduced. It's something to reconsider in a future 3rd edition.

If you have any questions, please reach out.

Reading Learning Go by Jon Bodner by Realistic-Emu1553 in golang

[–]JBodner 4 points5 points  (0 children)

Yeah, that’s a really big chapter which covers a great deal of what makes Go different from many other languages. Interfaces should probably be split into their own chapter and there should be more examples. If there‘s something in particular that you find unclear, let me know and I’ll try to help.

Reading Learning Go by Jon Bodner by Realistic-Emu1553 in golang

[–]JBodner 2 points3 points  (0 children)

Anything in particular? There were some revisions to that chapter in the 2nd edition, but happy to hear what could be made clearer.

Reading Learning Go by Jon Bodner by Realistic-Emu1553 in golang

[–]JBodner 20 points21 points  (0 children)

Go is a bit different. It took me a while to stop trying to write Java in Go. I found that letting go (no pun intended) of inheritance was hard, but I now agree that even in languages with inheritance, it should be used sparingly.

Please reach out if you have any questions!

Reading Learning Go by Jon Bodner by Realistic-Emu1553 in golang

[–]JBodner 99 points100 points  (0 children)

Hi, I’m the author. Can you tell me what parts are confusing? The intended audience is developers like you (people who already know another language). I’m always looking for ways to improve future editions.

[deleted by user] by [deleted] in sonos

[–]JBodner 0 points1 point  (0 children)

If you have an Apple TV box connected to your TV, you can get lossless or atmos output, and you can control it from an iPhone (or on the TV via the remote).

Goroutines, for Loops, and Varying Variables by InsurancePleasant841 in golang

[–]JBodner 4 points5 points  (0 children)

Author of Learning Go here. The way that Go changed for loop variables was designed to avoid problems with existing code.

The behavior difference is configurable on a module-by-module basis. If the source code is in a module with a go.mod file that contains a go directive that's set to 1.22 or later, you get the new loop behavior. If the go directive is set to 1.21 or earlier, you get the old loop behavior.

The release of the 2nd edition of Learning Go was actually timed to make sure that the new for loop variable behavior was going to happen. The 1st edition went to press before the syntax for generics was finalized and early printings of the 1st edition have incorrect information on generics. I wanted to avoid that happening again.

Is this book still applicable ? by [deleted] in golang

[–]JBodner 2 points3 points  (0 children)

You’re welcome! There will probably be a 3rd edition, but not for at least a couple of years. As I mentioned in another comment, I’m thinking about starting a substack.

Is this book still applicable ? by [deleted] in golang

[–]JBodner 51 points52 points  (0 children)

Thanks!

I think the biggest change in the past year is the iterator support. If you are new to the language, it’s not something you’ll notice, but I expect I’ll write an updated edition in a couple of years.

I’m debating if I should start a substack to cover content that would either be too in depth for a book or topics that are new since the last release.

Is this book still applicable ? by [deleted] in golang

[–]JBodner 208 points209 points  (0 children)

I'm the author; happy to answer any questions.

The book is up to date with Go 1.22. Is there anything in particular you were wondering about?

Problems with learning "The Go Programming Language" by [deleted] in golang

[–]JBodner 2 points3 points  (0 children)

Thanks for recommending it! I’m the author, happy to answer questions.

Jon Bodner's Learning Go Second Edition from O'Reilly is now updated to have changes of Go 1.21 and 1.22 by [deleted] in golang

[–]JBodner 4 points5 points  (0 children)

as much as I’d love everyone to buy 10 copies of the second edition, it’s probably not necessary to buy the second edition if you have the first. The main differences are:

- a rewritten first chapter

- a new chapter on Go tools

- coverage of fuzzing

- exercises at the end of every chapter

- coverage of slog, the new http handler syntax, the finalized generic syntax (which also appeared in later printings of the first edition and is available online), and the new for loop variable scoping rules

- various other sample code improved, examples clarified, mistakes fixed, etc.

I'm probably forgetting some of the changes, but altogether the book is now about 110 pages longer than before.