--
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/CAOyqgcUuv_qrrG8%3DdCQZv0%2BrKbnbW60XdOCwjp8M3EdOCxCNkw%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/a891bbf5-9426-49b3-89c6-f185fe047b5en%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAOXNBZSKudZatL9tjO%2BPSxKycp-4hEBwXDvO9z0bQtrCLSsk6w%40mail.gmail.com.
When you assign a regular (non-pointer) value to an interface variable, it does take a copy of that value:
As to whether the value is copied when you copy a non-pointer interface value to another interface variable, I'm having a hard time finding any way to demonstrate it one way or the other. If you write a mutator method then it needs to take a pointer (which a non-pointer value doesn't satisfy). And I can't find a simple way to get a pointer to the struct itself or its member, when it's held inside an interface value. Maybe it's possible via reflect?
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/3e3f8b10-3806-4959-9f44-8fe484f67a24n%40googlegroups.com.
For example, the fact that this code is broken is not intuitively obvious for any reader. It requires way too much scrutiny IMO.
https://play.golang.org/p/-f73t_Pm7ur
However, I see lots of calls of "If you're using pointers to interfaces a lot, you probably don't understand them".Well, what am I not understanding?
I recently translated a substantial C library into Go, and watching all the pointers disappear, at least syntactically (there were still slices), was marvelous.
On Jun 6, 2021, at 11:10 AM, jake...@gmail.com <jake...@gmail.com> wrote:
--
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/a9b4a8b3-0b2f-4935-807e-1cbca03a3b20n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/7C376006-EFA0-44BA-A036-B768078E4AA4%40ix.netcom.com.
On Jun 7, 2021, at 9:19 AM, '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/CAEkBMfGYRdadYFidvhCKnNqKDeqBSJLWBEU%3DuCJrxr1SbhPepQ%40mail.gmail.com.
I think that is my point. The methods in the code I shared have a proper receiver type based on the requirements of the methods.
So it seems to me that go lint should at least complain that a struct has mutating methods so all methods should take pointer receivers.
On Jun 7, 2021, at 12:57 PM, Axel Wagner <axel.wa...@googlemail.com> wrote:
I don’t think that represents the problem fairly. In the non interface case I know I can’t accept a copy so I would declare the method as taking a pointer to the struct.
With interfaces this is lost - as the interface is implicitly a pointer
On Jun 7, 2021, at 6:05 PM, Axel Wagner <axel.wa...@googlemail.com> wrote:
On Jun 7, 2021, at 6:16 PM, 'Axel Wagner' via golang-nuts <golan...@googlegroups.com> wrote:
--
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/CAEkBMfGVyvYYpQhCp_JkxN9EvgZ4FXJ8_WpxseJOB1OR7qt6ww%40mail.gmail.com.
I think that is why it is inconsistent and obtuse to me. The Log() method doesn’t need a pointer receiver. It works fine without it.
It only needs a pointer receiver because when passed to a function declared as taking an interface a copy is made (and a reference to the copy held). This copy is implicit not explicit.
This is a difficult model when working on large multi person projects - thus the “best practice” of using only pointer or value receivers I think. Suggesting that the linter flags code not adhering to this seems to have very little downside if any.
The pattern of a background stats collector is a common one. The atomic is required not optional.
On Jun 7, 2021, at 6:34 PM, Axel Wagner <axel.wa...@googlemail.com> wrote:
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAEkBMfHibUpDHQU9m8%3DrLYtnDj%3DFY01nkuP4k0Giow-hCbhNgQ%40mail.gmail.com.
We agree. It needs a pointer receiver to work. The atomic is also needed in this case for background logging.The problem in this case is that recordEvents() has to document that the EventLogger passed to recordEvents() must have a pointer receiver for the Log() method. There is nothing in the language that allows me to declare it nor the compiler to enforce it.
If you don’t see this as suboptimal and an area for improvement I am not sure what else I can say.
And by the way, linters often flag correct code - that is why they have disable options. They try to enforce the most common cases - and by the recommendation in the faq to only use receivers of the same type - it seems appropriate to me to have the linter flag this.
As to this being in my opinion the most inconsistent and obtuse aspect of Go - that is my opinion. Curious, what do you think would take the top spot?
On Jun 7, 2021, at 7:40 PM, Axel Wagner <axel.wa...@googlemail.com> wrote:
On Jun 7, 2021, at 8:18 PM, Robert Engels <ren...@ix.netcom.com> wrote:
Now, I have a struct I want to use with as an EventLogger (badly named - really EventSource). The code I wrote works fine. Test cases (of Log()) work fine.
I like Go. A lot. I’ve designed and built systems with millions of LOC. Pointing out aspects that might benefit from changes should be encouraged - if not it’s a religion not a programming language.
You say that test cases of Log work fine, but they are only fine in a
non-concurrent environment. The instant you test Log (without
interfaces) in a concurrent program it fails in an obvious manner.
Interfaces, and their ability to potentially be satisfied by either a
non-pointer type or a pointer type, depending on the type, is not the
problem here.
...Marvin
--
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/20210608043609.gguu3t3hqbsziema%40basil.wdw.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAEkBMfGa5B99OO8oTxJOWFSuJPinPF3b1QJWEuqMD4ZPAiCi%3DQ%40mail.gmail.com.