Dan & Bruno, I didn't realize that it is as simple as that :) Thanks.
Does it involve any extra copy of the byte slice? Or f.Write literally access the memory of d and only access 2 bytes of data for writing to the buffer? On Thu, Jan 18, 2018 at 5:13 PM, Dan Kortschak <dan.kortsc...@adelaide.edu.au> wrote: > If you want to write a length shorter than the slice, make the slice > shorter. No, seriously... > > n, err := f.Write(d[:2]) > > On Thu, 2018-01-18 at 17:04 -0600, Peng Yu 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 >> -- 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.