Re: [go-nuts] why rot13Reader example run twice?

2017-06-30 Thread Franco Marchesini
Now that I looked at the source, I learned.

// Reader is the interface that wraps the basic Read method.  //  // Read reads 
up to len(p) bytes into p. It returns the number of bytes  // read (0 <= n <= 
len(p)) and any error encountered. Even if Read  // returns n < len(p), it may 
use all of p as scratch space during the call.  // If some data is available 
but not len(p) bytes, Read conventionally  // returns what is available instead 
of waiting for more.  //  // When Read encounters an error or end-of-file 
condition after  // successfully reading n > 0 bytes, it returns the number of  
// bytes read. It may return the (non-nil) error from the same call  // or 
return the error (and n == 0) from a subsequent call.  // An instance of this 
general case is that a Reader returning  // a non-zero number of bytes at the 
end of the input stream may  // return either err == EOF or err == nil. The 
next Read should  // return 0, EOF.  //  // Callers should always process the n 
> 0 bytes returned before  // considering the error err. Doing so correctly 
handles I/O errors  // that happen after reading some bytes and also both of 
the  // allowed EOF behaviors.  //  // Implementations of Read are discouraged 
from returning a  // zero byte count with a nil error, except when len(p) == 0. 
 // Callers should treat a return of 0 and nil as indicating that  // nothing 
happened; in particular it does not indicate EOF.

Sorry for bother.
Thank you all.

Il giorno venerdì 30 giugno 2017 15:01:55 UTC+2, Jan Mercl ha scritto:
>
>
> On Fri, Jun 30, 2017 at 2:42 PM Franco Marchesini  > wrote:
>
> > Aside from the correction, however, I did not understand why the method 
> is called 2 times.
>
> HTH: https://play.golang.org/p/Na7GtgZ-_B
>
>
> -- 
>
> -j
>

-- 
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.


Re: [go-nuts] why rot13Reader example run twice?

2017-06-30 Thread Jan Mercl
On Fri, Jun 30, 2017 at 2:42 PM Franco Marchesini <
franco.marches...@gmail.com> wrote:

> Aside from the correction, however, I did not understand why the method
is called 2 times.

HTH: https://play.golang.org/p/Na7GtgZ-_B


-- 

-j

-- 
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.


Re: [go-nuts] why rot13Reader example run twice?

2017-06-30 Thread Franco Marchesini
Aside from the correction, however, I did not understand why the method is 
called 2 times.

Il giorno venerdì 30 giugno 2017 12:51:20 UTC+2, Franco Marchesini ha 
scritto:
>
> Yes, after Dave's answer I realized the mistake.
> This is a solution.
>
> Il giorno venerdì 30 giugno 2017 11:42:45 UTC+2, Sebastien Binet ha 
> scritto:
>>
>>
>>
>> On Fri, Jun 30, 2017 at 11:38 AM, Franco Marchesini <
>> franco.m...@gmail.com> wrote:
>>
>>> 1.
>>>
>>> Because with 2 there is a problem. The method is called twice and is 
>>> inefficient.
>>>
>>
>> what about checking your errors, then ? :)
>>  https://play.golang.org/p/K8j8D0AZe9
>>
>> -s
>>
>>
>>> Il giorno venerdì 30 giugno 2017 11:31:31 UTC+2, Jan Mercl ha scritto:

 On Fri, Jun 30, 2017 at 9:48 AM Franco Marchesini <
 franco.m...@gmail.com> wrote:

 > Why after the execution the k value is 2?

 What value of k do you expect instead and why? (
 https://play.golang.org/p/wdWo3fahAS)

 -- 

 -j

>>> -- 
>>> 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.
>>> 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.


Re: [go-nuts] why rot13Reader example run twice?

2017-06-30 Thread Franco Marchesini
Yes, after Dave's answer I realized the mistake.
This is a solution.

Il giorno venerdì 30 giugno 2017 11:42:45 UTC+2, Sebastien Binet ha scritto:
>
>
>
> On Fri, Jun 30, 2017 at 11:38 AM, Franco Marchesini  > wrote:
>
>> 1.
>>
>> Because with 2 there is a problem. The method is called twice and is 
>> inefficient.
>>
>
> what about checking your errors, then ? :)
>  https://play.golang.org/p/K8j8D0AZe9
>
> -s
>
>
>> Il giorno venerdì 30 giugno 2017 11:31:31 UTC+2, Jan Mercl ha scritto:
>>>
>>> On Fri, Jun 30, 2017 at 9:48 AM Franco Marchesini  
>>> wrote:
>>>
>>> > Why after the execution the k value is 2?
>>>
>>> What value of k do you expect instead and why? (
>>> https://play.golang.org/p/wdWo3fahAS)
>>>
>>> -- 
>>>
>>> -j
>>>
>> -- 
>> 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 .
>> 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.


Re: [go-nuts] why rot13Reader example run twice?

2017-06-30 Thread Sebastien Binet
On Fri, Jun 30, 2017 at 11:38 AM, Franco Marchesini <
franco.marches...@gmail.com> wrote:

> 1.
>
> Because with 2 there is a problem. The method is called twice and is
> inefficient.
>

what about checking your errors, then ? :)
 https://play.golang.org/p/K8j8D0AZe9

-s


> Il giorno venerdì 30 giugno 2017 11:31:31 UTC+2, Jan Mercl ha scritto:
>>
>> On Fri, Jun 30, 2017 at 9:48 AM Franco Marchesini 
>> wrote:
>>
>> > Why after the execution the k value is 2?
>>
>> What value of k do you expect instead and why? (
>> https://play.golang.org/p/wdWo3fahAS)
>>
>> --
>>
>> -j
>>
> --
> 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.


Re: [go-nuts] why rot13Reader example run twice?

2017-06-30 Thread Franco Marchesini
1.

Because with 2 there is a problem. The method is called twice and is 
inefficient.

Il giorno venerdì 30 giugno 2017 11:31:31 UTC+2, Jan Mercl ha scritto:
>
> On Fri, Jun 30, 2017 at 9:48 AM Franco Marchesini  > wrote:
>
> > Why after the execution the k value is 2?
>
> What value of k do you expect instead and why? (
> https://play.golang.org/p/wdWo3fahAS)
>
> -- 
>
> -j
>

-- 
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.


Re: [go-nuts] why rot13Reader example run twice?

2017-06-30 Thread Jan Mercl
On Fri, Jun 30, 2017 at 9:48 AM Franco Marchesini <
franco.marches...@gmail.com> wrote:

> Why after the execution the k value is 2?

What value of k do you expect instead and why? (
https://play.golang.org/p/wdWo3fahAS)

-- 

-j

-- 
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 rot13Reader example run twice?

2017-06-30 Thread Dave Cheney
Oh, sorry, I know. It'll be because the first time through your reader returns 
, nil, and on the second time returns 0, io.EOF. So Read is called twice, but 
on the second time does no work.

-- 
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 rot13Reader example run twice?

2017-06-30 Thread Dave Cheney
Print out the size of p each time Read is called

-- 
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 rot13Reader example run twice?

2017-06-30 Thread Franco Marchesini
Hello,
why the method Read of rot13Reader run twice?
This is the code:

package main

import (
"fmt"
"io"
"os"
"strings"
)

type rot13Reader struct {
r io.Reader
}

var k int

func (rot *rot13Reader) Read(p []byte) (n int, err error) {
k++
n, err = rot.r.Read(p)
for i := 0; i < len(p); i++ {
if (p[i] >= 'A' && p[i] < 'N') || (p[i] >= 'a' && p[i] < 'n') {
p[i] += 13
} else if (p[i] > 'M' && p[i] <= 'Z') || (p[i] > 'm' && p[i] <= 
'z') {
p[i] -= 13
}
}
return
}

func main() {
s := strings.NewReader("Lbh penpxrq gur pbqr!")
r := rot13Reader{s}
io.Copy(os.Stdout, )
fmt.Println("\n", k)
}


Why after the execution the k value is 2?

T.i.a
Franco

-- 
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.