We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
used go version 1.8.3 go env shows: GOARCH="amd64" GOBIN="/Users/franks/Documents/Development/sandbox/franks/go/bin" GOEXE="" GOHOSTARCH="amd64" GOHOSTOS="darwin" GOOS="darwin" GOPATH="/Users/franks/Documents/Development/sandbox/franks/go" GORACE="" GOROOT="/usr/local/go" GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64" GCCGO="gccgo" CC="clang" GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/y1/6h37hs7h8xj9vn001s7c4_h00000gn/T/go-build852796593=/tmp/go-build -gno-record-gcc-switches -fno-common" CXX="clang++" CGO_ENABLED="1" PKG_CONFIG="pkg-config" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2"
the following code shows that after closing a file and then rewriting to that file results in a file still holding the tail of the first write.
Code:
package main import ( "bufio" "io" "log" "os" "os/exec" ) func doWrite(aContent string) error { f, err := os.OpenFile("content.txt", os.O_WRONLY|os.O_CREATE, 0600) if err != nil { return err } defer f.Close() _, err = f.WriteString(aContent + "\n") return err } func doRead() (string, error) { f, err := os.Open("content.txt") if err != nil { return "", err } defer f.Close() buf := bufio.NewReader(f) content := "" doRead := true for doRead { line, err := buf.ReadString('\n') if err != nil { if err != io.EOF { return "", err } doRead = false } content = content + line } return content, nil } func main() { // show go version first: cmd := exec.Command("go", "version") version, err := cmd.CombinedOutput() if err != nil { log.Fatalf("%s", err) } log.Printf("%s", string(version)) // first write a long string to "content.txt" if err = doWrite("abcdefghijklmnopqrstuvwxyz"); err != nil { log.Fatalf("doWrite error (1): %s", err) } // read and show the contents: content, err := doRead() if err != nil { log.Fatalf("doRead error (1): %s", err) } log.Printf("doRead content (1): %q", content) // now write a short string again to "content.txt" if err = doWrite("abc"); err != nil { log.Fatalf("doWrite error (2): %s", err) } // read and show the contents: content, err = doRead() if err != nil { log.Fatalf("doRead error (2): %s", err) } log.Printf("doRead content (2): %q", content) }
the program generates:
2017/06/27 17:50:26 go version go1.8.3 darwin/amd64 2017/06/27 17:50:26 doRead content (1): "abcdefghijklmnopqrstuvwxyz\n" 2017/06/27 17:50:26 doRead content (2): "abc\nefghijklmnopqrstuvwxyz\n"
I Expected:
2017/06/27 17:50:26 go version go1.8.3 darwin/amd64 2017/06/27 17:50:26 doRead content (1): "abcdefghijklmnopqrstuvwxyz\n" 2017/06/27 17:50:26 doRead content (2): "abc\n" "abc\n"
but the program shows
"abc\nefghijklmnopqrstuvwxyz\n"
The text was updated successfully, but these errors were encountered:
Closing a file descriptor doesn't delete the file.
And os.O_WRONLY|os.O_CREATE doesn't mean to replace the file. See https://golang.org/pkg/os/#pkg-constants or http://pubs.opengroup.org/onlinepubs/009695399/functions/open.html for more details. (Go's os.OpenFile is a thin wrapper around the operating system's functionality)
os.O_WRONLY|os.O_CREATE
To delete the file, use os.Remove.
os.Remove
For questions about Go, see https://golang.org/wiki/Questions.
Sorry, something went wrong.
No branches or pull requests
used go version 1.8.3
go env shows:
GOARCH="amd64"
GOBIN="/Users/franks/Documents/Development/sandbox/franks/go/bin"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/franks/Documents/Development/sandbox/franks/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/y1/6h37hs7h8xj9vn001s7c4_h00000gn/T/go-build852796593=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
the following code shows that after closing a file and then rewriting to that file results in a file still
holding the tail of the first write.
Code:
the program generates:
I Expected:
but the program shows
The text was updated successfully, but these errors were encountered: