Re: [go-nuts] Why is ioutil.Discard implemented using an int and not an empty struct?

2016-10-17 Thread 'Axel Wagner' via golang-nuts
There is no semantic difference.

Disclaimer: On everything that follows, I'm not an expert (and it seems I'm
wrong. Not sure exactly how, though).

Though there *might* be a (*very*) slight advantage to using a struct{}.
All struct{}'s have the same address in gc, so if you put a struct{} into
an interface, that interface can be copied and compared directly.
If you put an int into an interface, it will need to be allocated
*somewhere* and you then put a pointer to it into the interface. And as, in
theory, the int could change, but a copy of the value shouldn't, it might
mean that when you assign ioutil.Discard to something else (or pass it to
some function as an io.Writer), that *may* incur an allocation.

However, the compiler might be clever enough to optimize that out with
inlining and escape analysis. And indeed, I'm failing to trigger the
behavior with any simple testcase. So I might be wrong here. Would be
interested in an experts explanation as to if that reasoning is
systematically broken :)

In any case, I don't think it'll make a practical difference.

On Mon, Oct 17, 2016 at 11:23 AM, Rodolfo Carvalho 
wrote:

> Hi,
>
> I noticed that when io.Discard was introduced in
> https://codereview.appspot.com/4426066/, it replaced code like:
>
> type devNull struct{}
>
>
> With:
>
>
> type devNull int
>
>
> Both had the very same implementation of the Write method:
>
> func (devNull) Write(p []byte) (int, os.Error) {
>  return len(p), nil
> }
>
>
>
> What's the advantage of using an int in this case, if any?
>
>
> Thank you,
>
> Rodolfo Carvalho
>
> --
> 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+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Why is ioutil.Discard implemented using an int and not an empty struct?

2016-10-17 Thread Rodolfo Carvalho
Hi,

I noticed that when io.Discard was introduced in
https://codereview.appspot.com/4426066/, it replaced code like:

type devNull struct{}


With:


type devNull int


Both had the very same implementation of the Write method:

func (devNull) Write(p []byte) (int, os.Error) {
 return len(p), nil
}



What's the advantage of using an int in this case, if any?


Thank you,

Rodolfo Carvalho

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.