Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

io: Copy clears source buffer #40840

Closed
matusf opened this issue Aug 17, 2020 · 3 comments
Closed

io: Copy clears source buffer #40840

matusf opened this issue Aug 17, 2020 · 3 comments

Comments

@matusf
Copy link

matusf commented Aug 17, 2020

What version of Go are you using (go version)?

$ go version
go version go1.14.2 linux/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/matus/.cache/go-build"
GOENV="/home/matus/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/matus/Code/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/matus/.local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/matus/.local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build176357012=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I tried to copy bytes from buffer to Stdout, however the buffer was cleared after copy. This is not a behaviour one would expect copy but rather from move. Like when you run cp src dst on linux, you do not expect src file to be cleared.

package main

import (
	"bytes"
	"fmt"
	"io"
	"log"
	"os"
)

func main() {
	b := bytes.Buffer{}
	n, err := b.Write([]byte("foo\n"))
	if err != nil {
		log.Fatalln(err, n)
	}

	fmt.Println(b)
	io.Copy(os.Stdout, &b)
	fmt.Println(b)
}

play.golang.org

What did you expect to see?

{[102 111 111 10] 0 0}
foo
{[102 111 111 10] 0 0}

or a clear note about this behaviour in the docs.

What did you see instead?

{[102 111 111 10] 0 0}
foo
{[] 0 0}
@davecheney
Copy link
Contributor

Thank you for raising this issue. Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For asking questions, see:

@matusf
Copy link
Author

matusf commented Aug 17, 2020

@davecheney this issue was not opened for asking question. I consider it a bug in a docs.

@davecheney
Copy link
Contributor

The buffer tracks both its read and write positions. The string method is simply returning you a slice of the unread data. The buffer has to keep track of the last read position otherwise if someone read from the buffer a byte at a time, the position would never advance.

@golang golang locked and limited conversation to collaborators Aug 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants