I have the same question, but using interface pointers. I've been having difficulty getting the following to work:
The map is still storing pointers to your struct at that point.
#2 - interfaces are effectively like pointers already, so you don't generally need/want to use pointers to interfaces.
#1 - pointers are your friend. Don't be afraid of them. Most of the time you'll be dealing with pointers to structs. It's no big deal.
#2 - interfaces are effectively like pointers already, so you don't generally need/want to use pointers to interfaces.
Thanks! Works like a charm and is helping cleaning up my code a ton.
One other question, this is really more about coding style:
In the case where you manipulate members of the struct, then using
pointers as in your example is the way to go.
But, you have a choice for functions that just read values from the
struct instead of manipulating it. Is there a best practice coding
style here, between dereferencing the struct and then using that, or
dereferencing each member of the struct as you go? eg:
// A:
laser := worldobj.(*Laser)
fmt.Printf("%0.4f,%0.4f", (*laser).x, (*laser).y)
versus
// B:
laser := *(worldobj.(*Laser))
fmt.Printf("%0.4f,%0.4f", laser.x, laser.y)
I'm kind of torn. I would imagine A) has slightly better
performance, and doesn't require any code-rework if you later on need
to manipulate the struct.
On the other hand, B) is more readable since you don't have to look at
pointers all over the place, just on one line.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAOyqgcX7v9Edk5beRH38tfJO18ZUXv-nOHsEPPCfMQy0hz%3DFdw%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/9b28006b-c310-417e-9afc-e7f5c470641cn%40googlegroups.com.
On Oct 7, 2024, at 4:43 PM, 'Axel Wagner' via golang-nuts <golan...@googlegroups.com> wrote:
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAEkBMfEj%3DQACB31VMc7ami7xt9tMF00kYxFUfZpWfZ0j65GWsw%40mail.gmail.com.
On Oct 7, 2024, at 5:10 PM, Axel Wagner <axel.wa...@googlemail.com> wrote:
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAEkBMfFYZ1DTD9fTVzNHtOp7Ed7w3_x8QbxsB2x_%2BTs%3DtxY0BA%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/B6F948A5-9F2E-4698-85D1-17B862779901%40ix.netcom.com.
On Oct 7, 2024, at 11:20 PM, Axel Wagner <axel.wa...@googlemail.com> wrote:
On Oct 7, 2024, at 11:20 PM, Axel Wagner <axel.wa...@googlemail.com> wrote:
On Oct 8, 2024, at 1:37 AM, 'Axel Wagner' via golang-nuts <golan...@googlegroups.com> wrote:
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/_MEf-I49OTo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAEkBMfGb7xEu%3Da73xWUBuAGK2T3_R7uA4K5FZYr4vYzkLpTxqg%40mail.gmail.com.
the only one on the "mixing receiver kinds is sometimes necessary, so we shouldn't caution against it" side of the table
On Tuesday 8 October 2024 at 07:37:25 UTC+1 Axel Wagner wrote:the only one on the "mixing receiver kinds is sometimes necessary, so we shouldn't caution against it" side of the tableTo me this sounds like a false dichotomy. It can be good general good-practice advice to avoid mixing pointer and value receivers (for various reasons including those raised by Robert Engels), and at the same time, in cases where it is necessary to do so, then clearly it has to be done (by definition of "necessary") as long as it's done with care ("caution").
The advice quoted is given under "Some useful guidelines" at https://go.dev/wiki/CodeReviewComments#receiver-type - it's not a hard-and-fast rule - and is aimed "especially to new Go programmers". If you know what you need and why, then that's fine.As another example: it's general good practice advice to accept interfaces and return concrete types. But for error values, it's necessary to return an interface value. That is an exception to the general rule, but doesn't invalidate it.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/f5be82e4-dd89-40c2-9603-f240748e23dcn%40googlegroups.com.
So can I say that, if I'm not writing concurrency code, it's acceptable for me to mix pointer and value receiver?
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/0f008c03-200a-4fa5-8198-3b1f0a227f9cn%40googlegroups.com.