diff --git a/README.md b/README.md index f02bdfc..6bd1334 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,26 @@ -# 📖 100 Go Mistakes and How to Avoid Them +# 100 Go Mistakes and How to Avoid Them -Source code and community space of [100 Go Mistakes and How to Avoid Them](https://www.manning.com/books/100-go-mistakes-and-how-to-avoid-them) published by Manning in October 2022 ([about the book](https://github.com/teivah/100-go-mistakes/blob/master/about.md)). Made by [@teivah](https://twitter.com/teivah). +Source code and community space of 📖 [100 Go Mistakes and How to Avoid Them](https://www.manning.com/books/100-go-mistakes-and-how-to-avoid-them) published by Manning in October 2022. Made by [@teivah](https://twitter.com/teivah). ![](cover.png) +100 Go Mistakes and How to Avoid Them shows you how to replace common programming problems in Go with idiomatic, expressive code. In it, you’ll explore dozens of interesting examples and case studies as you learn to spot mistakes that might appear in your own applications. + +> This should be the required reading for all Golang developers before they touch code in Production... It's the Golang equivalent of the legendary 'Effective Java' by Joshua Bloch. + +– Neeraj Shah + +> "This unique book teaches you good habits by helping you identify bad ones. Harsanyi's writing style is engaging, the examples relevant, and his insights useful. I thought it was a great read, and I think you will too." + +– Thad Meyer + +> "Not having this will be the 101st mistake a Go programmer could make." + +– Anupam Sengupta + ## Common Go Mistakes -🌎 This section contains the 100 mistakes in the book. However, it's also a section open to the community. If you believe that a mistake should be added, please create a [community mistake issue](https://github.com/teivah/100-go-mistakes/issues/new?assignees=&labels=community+mistake&template=community_mistake.md&title=). You can check the community propositions [here](https://github.com/teivah/100-go-mistakes/issues?q=is%3Aopen+is%3Aissue+label%3A%22community+mistake%22) and vote using the 👍 or the 👎 reactions on the issue itself. If an issue reaches a consensus, it will be added to the corresponding section below. +This section contains a summary of the 100 mistakes in the book. Meanwhile, it's also a section open to the community. If you believe that a mistake should be added, please create a [community mistake issue](https://github.com/teivah/100-go-mistakes/issues/new?assignees=&labels=community+mistake&template=community_mistake.md&title=). * [Code and Project Organization](#code-and-project-organization) * [Data Types](#data-types) @@ -87,7 +101,7 @@ To help clients and maintainers understand your code’s purpose, document expor To improve code quality and consistency, use linters and formatters. ### Data Types - + #### Creating confusion with octal literals (#17) When reading existing code, bear in mind that integer literals starting with 0 are octal numbers. Also, to improve readability, make octal integers explicit by prefixing them with `0o`. @@ -98,7 +112,7 @@ Because integer overflows and underflows are handled silently in Go, you can imp #### Not understanding floating-points (#19) -Making floating-point comparisons within a given delta can ensure that your code is portable. +Making floating-point comparisons within a given delta can ensure that your code is portable. When performing addition or subtraction, group the operations with a similar order of magnitude to favor accuracy. Also, perform multiplication and division before addition and subtraction. @@ -144,7 +158,7 @@ A map can always grow in memory, but it never shrinks. Hence, if it leads to som To compare types in Go, you can use the == and != operators if two types are comparable: Booleans, numerals, strings, pointers, channels, and structs are composed entirely of comparable types. Otherwise, you can either use `reflect.DeepEqual` and pay the price of reflection or use custom implementations and libraries. -### Control Structures +### Control Structures #### Ignoring that elements are copied in `range` loops (#30) @@ -175,7 +189,7 @@ Using `break` or `continue` with a label enforces breaking a specific statement. Extracting loop logic inside a function leads to executing a `defer` statement at the end of each iteration. ### Strings - + #### Not understanding the concept of rune (#36) Understanding that a rune corresponds to the concept of a Unicode code point and that it can be composed of multiple bytes should be part of the Go developer’s core knowledge to work accurately with strings. @@ -201,7 +215,7 @@ Remembering that the `bytes` package offers the same operations as the `strings` Using copies instead of substrings can prevent memory leaks, as the string returned by a substring operation will be backed by the same byte array. ### Functions and Methods - + #### Not knowing which type of receiver to use (#42) The decision whether to use a value or a pointer receiver should be made based on factors such as the type, whether it has to be mutated, whether it contains a field that can’t be copied, and how large the object is. When in doubt, use a pointer receiver. @@ -225,7 +239,7 @@ Designing functions to receive `io.Reader` types instead of filenames improves t #### Ignoring how `defer` arguments and receivers are evaluated (argument evaluation, pointer, and value receivers) (#47) Passing a pointer to a `defer` function and wrapping a call inside a closure are two possible solutions to overcome the immediate evaluation of arguments and receivers. - + ### Error Management #### Panicking (#48) @@ -259,7 +273,7 @@ Ignoring an error, whether during a function call or in a `defer` function, shou In many cases, you shouldn’t ignore an error returned by a `defer` function. Either handle it directly or propagate it to the caller, depending on the context. If you want to ignore it, use the blank identifier. ### Concurrency: Foundations - + #### Mixing up concurrency and parallelism (#55) Understanding the fundamental differences between concurrency and parallelism is a cornerstone of the Go developer’s knowledge. Concurrency is about structure, whereas parallelism is about execution. @@ -314,7 +328,7 @@ Using nil channels should be part of your concurrency toolset because it allows #### Being puzzled about channel size (#67) -Carefully decide on the right channel type to use, given a problem. Only unbuffered channels provide strong synchronization guarantees. +Carefully decide on the right channel type to use, given a problem. Only unbuffered channels provide strong synchronization guarantees. You should have a good reason to specify a channel size other than one for buffered channels. @@ -532,3 +546,19 @@ Understanding how to tune the GC can lead to multiple benefits such as handling #### Not understanding the impacts of running Go in Docker and Kubernetes (#100) To help avoid CPU throttling when deployed in Docker and Kubernetes, keep in mind that Go isn’t CFS-aware. + +## Inside Cover + +![](inside-cover.jpg) + +## About the Author + +Teiva Harsanyi is a senior software engineer at Docker. He worked in various domains, including insurance, transportation, and safety-critical industries like air traffic management. He is passionate about Go and how to design and implement reliable applications. + +## Resources + +* How to make mistakes in Go (Go Time - episode #190) + * [Episode](https://changelog.com/gotime/190) + * [Spotify](https://open.spotify.com/episode/0K1DImrxHCy6E7zVY4AxMZ?si=akroInsPQ1mM5B5V2tHLUw&dl_branch=1) + +* 8LU - 100% Test Coverage: [YouTube](https://youtu.be/V3FBDav6wgQ?t=1210) diff --git a/about.md b/about.md deleted file mode 100644 index a288bc6..0000000 --- a/about.md +++ /dev/null @@ -1,81 +0,0 @@ -# About the Book: 100 Go Mistakes and How to Avoid Them - -https://www.manning.com/books/100-go-mistakes-and-how-to-avoid-them (-35% with the code **au35har**) - -Spot errors in your Go code you didn’t even know you were making and boost your productivity by avoiding common mistakes and pitfalls. - -![](inside-cover.jpg) - -_100 Go Mistakes and How to Avoid Them_ shows you how to: - -* Dodge the most common mistakes made by Go developers -* Structure and organize your Go application -* Handle data and control structures efficiently -* Deal with errors in an idiomatic manner -* Improve your concurrency skills -* Optimize your code -* Make your application production-ready and improve testing quality - - -_100 Go Mistakes and How to Avoid Them_ puts a spotlight on common errors in Go code you might not even know you’re making. You’ll explore key areas of the language such as concurrency, testing, data structures, and more—and learn how to avoid and fix mistakes in your own projects. As you go, you’ll navigate the tricky bits of handling JSON data and HTTP services, discover best practices for Go code organization, and learn how to use slices efficiently. - -## About the Technology - -Understanding mistakes is the best way to improve the quality of your code. This unique book examines 100 bugs and inefficiencies common to Go applications, along with tips and techniques to avoid making them in your own projects. - -## About the Book - -_100 Go Mistakes and How to Avoid Them_ shows you how to replace common programming problems in Go with idiomatic, expressive code. In it, you’ll explore dozens of interesting examples and case studies as you learn to spot mistakes that might appear in your own applications. Expert author Teiva Harsanyi organizes the error avoidance techniques into convenient categories, ranging from types and strings to concurrency and testing. - -## Table of Contents - -1. Go: Simple to learn but hard to master -2. Code and project organization -3. Data types -4. Control structures -5. Strings -6. Functions and methods -7. Error management -8. Concurrency: Foundations -9. Concurrency: Practice -10. Standard library -11. Testing -12. Optimizations - -## About the Reader - -For developers proficient with Go programming and syntax. - -## About the Author - -Teiva Harsanyi is a senior software engineer at Docker. He worked in various domains, including insurance, transportation, and safety-critical industries like air traffic management. He is passionate about Go and how to design and implement reliable applications. - -## Quotes - -> "This should be the required reading for all Golang developers before they touch code in Production... It's the Golang equivalent of the legendary 'Effective Java' by Joshua Bloch." - -– Neeraj Shah - -> "This unique book teaches you good habits by helping you identify bad ones. Harsanyi's writing style is engaging, the examples relevant, and his insights useful. I thought it was a great read, and I think you will too." - -– Thad Meyer - -> "Learning from mistakes is proven as one of the best ways to learn a subject. This book helps you do just that by demonstrating the most common mistakes people make coming to Go, why most people make them and the proper way to solve the problems." - -– Ryan Huber - -> "This book explains many subtleties of the Go programming language that may cause errors and provides the reader with advice on how to deal with these situations. The precise explanations and real world examples make it a great addition for those learning Go programming language or looking to advance their mastery of the language." - -– Borko Djurkovic - -> "Not having this will be the 101st mistake a Go programmer could make." - -– Anupam Sengupta - -## Resources - -* How to make mistakes in Go (Go Time - episode #190) - * [Episode](https://changelog.com/gotime/190) - * [Spotify](https://open.spotify.com/episode/0K1DImrxHCy6E7zVY4AxMZ?si=akroInsPQ1mM5B5V2tHLUw&dl_branch=1) - -* 8LU - 100% Test Coverage: [YouTube](https://youtu.be/V3FBDav6wgQ?t=1210) diff --git a/cover.png b/cover.png index 94b36fb..33422b7 100644 Binary files a/cover.png and b/cover.png differ