That's not necessary. As it says in the last part of the quoted
documentation,

    In most cases, new(Buffer) (or just declaring a Buffer variable) is
    sufficient to initialize a Buffer.

Using NewBuffer to create an empty buffer is a mistake.

-rob


On Wed, Oct 11, 2017 at 2:47 AM, Marvin Stenger <marvin.stenge...@gmail.com>
wrote:

> $ go doc bytes NewBuffer
> func NewBuffer(buf []byte) *Buffer
>     NewBuffer creates and initializes a new Buffer using buf as its initial
>     contents. The new Buffer takes ownership of buf, and the caller should
> not
>     use buf after this call. NewBuffer is intended to prepare a Buffer to
> read
>     existing data. It can also be used to size the internal buffer for
> writing.
>     To do that, buf should have the desired capacity but a length of zero.
>
>     In most cases, new(Buffer) (or just declaring a Buffer variable) is
>     sufficient to initialize a Buffer.
>
> so either you could do
> buf:=bytes.NewBuffer(make([]byte,0,4))
> or
> buf:=bytes.NewBuffer(make([]byte,4))
> buf.Reset()
>
>
> Am Dienstag, 10. Oktober 2017 10:52:21 UTC+2 schrieb Aurelien RAINONE:
>>
>> Hi everybody,
>>
>> I ran into this problem yesterday, I couldn't convert a string created
>> with a bytes.Buffer into a big.Int by using SetString. On the other hand,
>> with the same string value, created from literals or even from a byte
>> slice, the conversion is successful.
>>
>> Find the code below or on the playground at https://play.golang.org/p/S
>> g7ShB-okP :
>>
>> package main
>>
>> import (
>> "bytes"
>> "fmt"
>> "math/big"
>> )
>>
>> func tryConvert(s string) {
>> var ok bool
>> bint := new(big.Int)
>> if bint, ok = bint.SetString(s, 10); !ok {
>> fmt.Println("\tFail")
>> } else {
>> fmt.Println("\tSuccess, proof: ", bint)
>> }
>> }
>>
>> func main() {
>> buf := bytes.NewBuffer(make([]byte, 4))
>> buf.WriteString("1234")
>>
>> fmt.Println("Conversion from bytes.Buffer.String() to big.Int:")
>> tryConvert(buf.String())
>>
>> fmt.Println("Conversion from string to big.Int:")
>> tryConvert("1234")
>>
>> fmt.Println("Conversion from string([]byte{}) to big.Int:")
>> tryConvert(string([]byte{byte('1'), byte('2'), byte('3'), byte('4')}))
>> }
>>
>>
>>
>> $ go version
>> go version go1.9.1 linux/amd64
>>
>>
>> What am I missing?
>>
>> Have a great day
>>
>> Aurélien Rainone
>>
>
> --
> 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.

Reply via email to