n, err := f.Write(d[:length]) ?

Where length is the amount of data you want to write.

On Thu, Jan 18, 2018 at 3:04 PM Peng Yu <pengyu...@gmail.com> wrote:

> Hi,
>
> The following example shows how to write a byte slice to a buffer. But
> what if I want only write a length shorter (say 2 instead of 5) than
> the size of the slice. Is there a function that allows users to
> specify the length to write? Thanks.
>
> $ cat main.go
> #!/usr/bin/env gorun
> // vim: set noexpandtab tabstop=2:
> package main
>
> import (
>     "os"
>     "bufio"
>     "fmt"
> )
>
> func main() {
>     f := bufio.NewWriter(os.Stdout)
>     d := []byte{'s', 'o', 'm', 'e', '\n'}
>     n, err := f.Write(d)
>     f.Flush()
>     fmt.Printf("wrote %d bytes, error %q\n", n, err)
> }
>
> --
> Regards,
> Peng
>
> --
> 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