Go structs are copied on assignment
The author shares insights on learning Go, emphasizing struct assignment, slice appending, and value versus pointer receivers. They recommend the "100 Go Mistakes" resource and other tools for better understanding.
Read original articleThe author reflects on their experiences with the Go programming language, highlighting some fundamental concepts they initially misunderstood. After encountering a bug related to struct assignment, they discovered that structs in Go are copied on assignment, which led to confusion when trying to modify a struct returned from a function. This realization prompted them to explore the "100 Go Mistakes and How To Avoid Them" resource, which helped clarify several misconceptions. They also learned about the implications of appending to slices, the differences between value and pointer receivers in methods, and various other common pitfalls in Go programming. The author appreciates the structured format of the resource, which allows for quick identification of useful information. They conclude by mentioning additional resources that have been helpful in their learning journey, including documentation and linters.
- Go structs are copied on assignment, leading to potential bugs when modifying returned values.
- Appending to slices can unintentionally affect the original slice due to shared backing arrays.
- Understanding value vs. pointer receivers is crucial for method behavior in Go.
- The "100 Go Mistakes" resource provides a concise way to learn from common errors.
- Additional resources like Go by Example and staticcheck are valuable for improving Go programming skills.
Related
Interface Upgrades in Go (2014)
The article delves into Go's interface upgrades, showcasing their role in encapsulation and decoupling. It emphasizes optimizing performance through wider interface casting, with examples from io and net/http libraries. It warns about complexities and advises cautious usage.
Common Interface Mistakes in Go
The article delves into interface mistakes in Go programming, stressing understanding of behavior-driven, concise interfaces. It warns against excessive, non-specific interfaces and offers guidance from industry experts for improvement.
First impressions of Go 1.23's range-over-func feature
The author shares positive experiences with Go 1.23's range-over-func feature, initially skeptical but finding it easy to use. Successful adaptation in their project Kivik disproved initial concerns, highlighting benefits for codebase improvement.
Don't write Rust like it's Java
The author discusses transitioning from Java to Rust, highlighting Rust's type safety, differences in traits and interfaces, complexities of ownership, and the importance of embracing Rust's unique features for effective programming.
Go structs are copied on assignment (and other things about Go I'd missed)
The author reflects on learning Go, highlighting misconceptions about struct assignment, sub-slice modifications, and method receivers. They recommend the "100 Go Mistakes" resource and other learning materials for improvement.
I wonder if it is possible to create a high-level imperative language that makes a clear difference between variables holding a value (possibly very complex with many different components, like lists, sets, trees, maps, and references) and cursors that point to a part of a value in a variable. Such cursors would typically always belong to a value in a variable and can be used to query and also modify the value.
Related
Interface Upgrades in Go (2014)
The article delves into Go's interface upgrades, showcasing their role in encapsulation and decoupling. It emphasizes optimizing performance through wider interface casting, with examples from io and net/http libraries. It warns about complexities and advises cautious usage.
Common Interface Mistakes in Go
The article delves into interface mistakes in Go programming, stressing understanding of behavior-driven, concise interfaces. It warns against excessive, non-specific interfaces and offers guidance from industry experts for improvement.
First impressions of Go 1.23's range-over-func feature
The author shares positive experiences with Go 1.23's range-over-func feature, initially skeptical but finding it easy to use. Successful adaptation in their project Kivik disproved initial concerns, highlighting benefits for codebase improvement.
Don't write Rust like it's Java
The author discusses transitioning from Java to Rust, highlighting Rust's type safety, differences in traits and interfaces, complexities of ownership, and the importance of embracing Rust's unique features for effective programming.
Go structs are copied on assignment (and other things about Go I'd missed)
The author reflects on learning Go, highlighting misconceptions about struct assignment, sub-slice modifications, and method receivers. They recommend the "100 Go Mistakes" resource and other learning materials for improvement.