What is the URL of the page with the issue?
https://go.dev/doc/effective_go#composite_literals
What is your user agent?
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36
Screenshot
What did you do?
I was reading the documentation on composite literals and noticed that the syntax *obj = MyStruct{} could be misinterpreted as allocating memory. This confusion has appeared in the community, including a Reddit discussion where developers using object pools believed they needed to manually clear struct fields. I conducted research to gather proof and clarify the behavior, but it was a bit hard to find it.
What did you expect to see?
A note or clarification that *obj = MyStruct{} does not allocate memory, but instead assigns a new value to an already allocated object, preventing misconceptions about memory clearing. While the documentation explains when memory is allocated in general, it does not mention this specific case where the compiler’s optimization avoids allocation.
What did you see instead?
The documentation currently does not mention that *obj = MyStruct{} does not perform memory allocation, which could lead to misunderstanding and unnecessary manual clearing of struct fields.
What is the URL of the page with the issue?
https://go.dev/doc/effective_go#composite_literals
What is your user agent?
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36
Screenshot
What did you do?
I was reading the documentation on composite literals and noticed that the syntax
*obj = MyStruct{}could be misinterpreted as allocating memory. This confusion has appeared in the community, including a Reddit discussion where developers using object pools believed they needed to manually clear struct fields. I conducted research to gather proof and clarify the behavior, but it was a bit hard to find it.What did you expect to see?
A note or clarification that
*obj = MyStruct{}does not allocate memory, but instead assigns a new value to an already allocated object, preventing misconceptions about memory clearing. While the documentation explains when memory is allocated in general, it does not mention this specific case where the compiler’s optimization avoids allocation.What did you see instead?
The documentation currently does not mention that
*obj = MyStruct{}does not perform memory allocation, which could lead to misunderstanding and unnecessary manual clearing of struct fields.