Skip to content

bufio: bufio.Write and bufio.WriteString behave differently #69068

@cccristi07

Description

@cccristi07

Go version

go version go1.23.0 darwin/arm64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/ccristi/Library/Caches/go-build'
GOENV='/Users/ccristi/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/ccristi/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/ccristi/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/opt/homebrew/Cellar/go/1.23.0/libexec'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/opt/homebrew/Cellar/go/1.23.0/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.23.0'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/ccristi/Library/Application Support/go/telemetry'
GCCGO='gccgo'
GOARM64='v8.0'
AR='ar'
CC='cc'
CXX='c++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/z2/2b0v421s24g_0d_fg2txybwm0000gn/T/go-build1898012219=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

Consider the following scenario:

Using WriteString

We see that the calls to the underlying io.Writer are buffered in calls with a buffer length of 3

type X struct {
	i io.Writer
}

func (x *X) Write(p []byte) (n int, err error) {
	fmt.Println("Writing ", len(p), "bytes to underlying writer")
	fmt.Println()
	n, err = x.i.Write(p)
	if err != nil {
		return
	}
	return
}

func main() {
	x := X{os.Stdout}

	bfw := bufio.NewWriterSize(&x, 3)
	//nn, err := bfw.Write([]byte("abcdefghijklmno..."))
	nn, err := bfw.WriteString("abcdefghijklmno...")
	if err != nil {
		fmt.Println("buffered writer tried writing ", nn)
		panic(err)
	}
}

yields the following result:

Writing  3 bytes to underlying writer

abc
Written  3 bytes to underlying writer
Writing  3 bytes to underlying writer

def
Written  3 bytes to underlying writer
Writing  3 bytes to underlying writer

ghi
Written  3 bytes to underlying writer
Writing  3 bytes to underlying writer

jkl
Written  3 bytes to underlying writer
Writing  3 bytes to underlying writer

mno
Written  3 bytes to underlying writer

Using bufio.Write

func main() {
	x := X{os.Stdout}

	bfw := bufio.NewWriterSize(&x, 3)
	nn, err := bfw.Write([]byte("abcdefghijklmno..."))
	//nn, err := bfw.WriteString("abcdefghijklmno...")
	if err != nil {
		fmt.Println("buffered writer tried writing ", nn)
		panic(err)
	}
}

We get the following output:

Writing  18 bytes to underlying writer

abcdefghijklmno...
Written  18 bytes to underlying writer

What did you see happen?

bufio.WriteString buffers calls to the underlying io.Writer in chunks of len(b.buf).

bufio.Write will not buffer calls to the underlying io.Writer but rather sends the whole buffer

What did you expect to see?

I was expecting to see bufio.Write behave in a similar way to bufio.WriteString.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions