Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-08 Thread Marvin Renich
* Robert Engels [210608 18:30]: > I think the playground code I supplied is essentially a test case - > and it works in the absence of concurrency or other held references > (like putting it in a map). But it is one simple test case, not an appropriate set of tests. Saying that code passes one

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-08 Thread Robert Engels
I think the playground code I supplied is essentially a test case - and it works in the absence of concurrency or other held references (like putting it in a map). I guess the bottom line for me is that faq says do not mix receiver types. There is either a valid reason for this or it should

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-08 Thread 'Dan Kortschak' via golang-nuts
On Tue, 2021-06-08 at 07:57 -0500, robert engels wrote: > The following code is works fine from the developers perspective: > > https://play.golang.org/p/gC1XsSLvovM > > The developer says, oh cool, I see this great new 3P library that > does background logging - I want to use that instead. Hey, I

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-08 Thread Marvin Renich
* 'Axel Wagner' via golang-nuts [210608 02:54]: > On Tue, Jun 8, 2021 at 6:36 AM Marvin Renich wrote: > > > 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

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-08 Thread robert engels
OK, I will try one more time. The following code is works fine from the developers perspective: https://play.golang.org/p/gC1XsSLvovM The developer says, oh cool, I see this great new 3P library that does background logging - I want to use that instead.

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-08 Thread 'Axel Wagner' via golang-nuts
On Tue, Jun 8, 2021 at 6:36 AM Marvin Renich wrote: > 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. > nit: I don't think concurrency has

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-08 Thread 'Axel Wagner' via golang-nuts
On Tue, Jun 8, 2021 at 3:16 AM Robert Engels wrote: > 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. > FTR, this is the second time you are

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-08 Thread 'Axel Wagner' via golang-nuts
On Tue, Jun 8, 2021 at 3:16 AM Robert Engels 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. > This is where the refusal to acknowledge comes in. It does not work fine. I

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-07 Thread Marvin Renich
* Robert Engels [210607 21:18]: > (I think you pasted the wrong link - that is my code). subsequent correction acknowledged; assuming the following discussion is about https://play.golang.org/p/-f73t_Pm7ur > It is not about being unwilling to admit it. Your > explanation/reasoning has not

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-07 Thread Robert Engels
Sorry - correct link. I missed the subtle change. > On Jun 7, 2021, at 8:18 PM, Robert Engels wrote: > >  > (I think you pasted the wrong link - that is my code). > > It is not about being unwilling to admit it. Your explanation/reasoning has > not convinced me. > > Imagine some library

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-07 Thread Robert Engels
(I think you pasted the wrong link - that is my code). It is not about being unwilling to admit it. Your explanation/reasoning has not convinced me. Imagine some library declares the EventLogger interface as shown. Acceptable. Someone writes the RecordEvents() method taking an EventLogger.

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-07 Thread Marvin Renich
* 'Axel Wagner' via golang-nuts [210607 19:06]: > Well, it seems a bad idea to say that interfaces are implicitly pointers > then. That seems to indicate that Rob's original phrasing is indeed an > important clarification - the language behaves as if the value contained in > them is copied when

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-07 Thread 'Axel Wagner' via golang-nuts
On Tue, Jun 8, 2021 at 2:05 AM Robert Engels wrote: > > 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

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-07 Thread Robert Engels
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

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-07 Thread 'Axel Wagner' via golang-nuts
On Tue, Jun 8, 2021 at 1:26 AM Robert Engels wrote: > The pattern of a background stats collector is a common one. The atomic is > required not optional. > It might be a common pattern, but it uses a pointer-receiver in that case. The atomic operation is not required, it operates on a local

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-07 Thread 'Axel Wagner' via golang-nuts
On Tue, Jun 8, 2021 at 1:25 AM Robert Engels wrote: > 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. > I don't understand how you can continue to say that. I've linked the code several times. It does not

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-07 Thread Robert Engels
The pattern of a background stats collector is a common one. The atomic is required not optional. > On Jun 7, 2021, at 6:16 PM, 'Axel Wagner' via golang-nuts > wrote: > >  > BTW, just to nail down the point of that code being wrong without interfaces: > Your usage of `atomic` in `Log` is

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-07 Thread Robert Engels
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

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-07 Thread 'Axel Wagner' via golang-nuts
BTW, just to nail down the point of that code being wrong without interfaces: Your usage of `atomic` in `Log` is superfluous. You are operating on a local variable, so there is no possibility of concurrent modification. Your code is equivalent to this: https://play.golang.org/p/zYG0zTsk-2a The

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-07 Thread 'Axel Wagner' via golang-nuts
On Mon, Jun 7, 2021 at 11:42 PM Robert Engels 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. > How methods are declared should, in general, not be a matter of

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-07 Thread Robert Engels
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 - but whether it points to a copy or the original

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-07 Thread Marvin Renich
* 'Axel Wagner' via golang-nuts [210607 10:19]: > FWIW I do tend to mix value and pointer receivers occasionally. > Sometimes a type needs a pointer receiver in one method, but a different > method doesn't and it's more convenient to have a copy available for > temporary operations. Axel, I

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-07 Thread 'Axel Wagner' via golang-nuts
On Mon, Jun 7, 2021 at 7:42 PM Robert Engels wrote: > 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. > No, they don't. The `Log` method requires a pointer receiver, as it accesses state that is supposed to be

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-07 Thread Robert Engels
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. It only “breaks” in the context of the usage which isn’t at all obvious. So it seems to me that go lint should at least complain that a struct has mutating methods

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-07 Thread 'Axel Wagner' via golang-nuts
FWIW I do tend to mix value and pointer receivers occasionally. Sometimes a type needs a pointer receiver in one method, but a different method doesn't and it's more convenient to have a copy available for temporary operations. Usually when I implement flag.Var, I make `Set` use a pointer receiver

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-07 Thread Robert Engels
There is no good reason that proper behavior should be dependent on understanding best practices. It should help with readability not correctness. Seems to me the compiler or Go Vet should prohibit this - in my review of the stdlib and other projects I can’t see any reason why it doesn’t. >

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-06 Thread ben...@gmail.com
> I recently translated a substantial C library into Go, and watching all > the pointers disappear, at least syntactically (there were still slices), > was marvelous. > Side point: Rob, is this open source? If so, I'd be interested to see the side-by-side comparison of the C vs Go code

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-06 Thread Marvin Renich
* 'Dan Kortschak' via golang-nuts [210606 06:43]: > On Sun, 2021-06-06 at 03:17 -0700, Brian Candler wrote: > > When you assign a regular (non-pointer) value to an interface > > variable, it does take a copy of that value: > > https://play.golang.org/p/XyBREDL4BGw > > It depends on whether it's

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-06 Thread jake...@gmail.com
On Sunday, June 6, 2021 at 9:33:31 AM UTC-4 ren...@ix.netcom.com wrote: > 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 > I would like to note that your example goes

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-06 Thread robert engels
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 > On Jun 6, 2021, at 6:37 AM, Robert Engels wrote: > > For me this is the most inconsistent and obtuse aspect of the Go

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-06 Thread Robert Engels
For me this is the most inconsistent and obtuse aspect of the Go language. It seems it would always be saner to treat interfaces as pointers. Which would mean if they had non pointer receiver methods might force more objects to be allocated on the heap - but it would prevent a lot of

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-06 Thread 'Dan Kortschak' via golang-nuts
On Sun, 2021-06-06 at 03:17 -0700, Brian Candler wrote: > When you assign a regular (non-pointer) value to an interface > variable, it does take a copy of that value: > https://play.golang.org/p/XyBREDL4BGw It depends on whether it's safe to leave uncopied or not. You can see this here

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-06 Thread 'Axel Wagner' via golang-nuts
On Sun, Jun 6, 2021 at 12:17 PM Brian Candler wrote: > When you assign a regular (non-pointer) value to an interface variable, it > does take a copy of that value: > https://play.golang.org/p/XyBREDL4BGw > Yupp, as I said :) > As to whether the value is copied when you copy a non-pointer

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-06 Thread Joshua
I can only give the opinion of someone who's new to the language, but maybe that is helpful for me to describe the learning journey a bit. I'd seen references (hehe) to maps and slices "acting like pointers" and thus weren't costly to be passing around directly into functions, and so I went to

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-06 Thread Brian Candler
When you assign a regular (non-pointer) value to an interface variable, it does take a copy of that value: https://play.golang.org/p/XyBREDL4BGw Compare with what happens when the interface contains a pointer: https://play.golang.org/p/UpZnHS0xDU1 As to whether the value is copied when you copy

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-06 Thread 'Axel Wagner' via golang-nuts
TBH from that FAQ answer I would have come to the same conclusion as OP. It literally says "Copying an interface value makes a copy of the thing stored in the interface value". But it doesn't. Assigning to an interface variable makes a copy of the value. Calling one of the methods on the

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-06 Thread Rob Pike
Can you explain the trap? I don't pick up that vibe, but I may be the author of that paragraph. Plus there is no such thing as a big interface. In the current implementation, all interfaces are the same size - a pair of words. You may still have a misapprehension. Try the first half of this

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-06 Thread Joshua
Thanks all for the insights, I think a key takeaway for me is "Don't worry about it unless it's a problem", but it's also good to know that it (probably) isn't a problem! I'm glad at least the semantics are the same, and I guess I'll cross the performance bridge if I ever come to it and

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-05 Thread Amnon
I find that people coming to Go from C++ tend to use pointers everywhere so as to avoid copying of structs. Once they get a bit more experience, they tend to use fewer pointers, and are happier to pass structs around. Removing the "make everything a pointer" optimisation makes the code simpler,

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-05 Thread 'Axel Wagner' via golang-nuts
I would add that because the dynamic type of an interface value is not known at compile time, a variable of interface type really can't (in general) have a specific size. If a function has an interface parameter, it must be possible to pass a value of *any* size to it. So even aside from what the

Re: [go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-05 Thread Ian Lance Taylor
On Sat, Jun 5, 2021 at 2:15 PM Joshua wrote: > > My question is general, but for ease of communicating I'll use the specific > example I ran into. > > I'm very new and for my first project I'm working with the bleve library > [https://pkg.go.dev/github.com/blevesearch/bleve]. > > One function I

[go-nuts] Knowing from documentation whether an interface is holding a pointer or a struct?

2021-06-05 Thread Joshua
Hello, My question is general, but for ease of communicating I'll use the specific example I ran into. I'm very new and for my first project I'm working with the bleve library [https://pkg.go.dev/github.com/blevesearch/bleve]. One function I need, "Open", returns an interface, "Index". I'd