在Windows上但在Linux上没有数据填充文件

I have made a small service application that writes it's output to several files. The service has to run on both Windows and Linux. Everything is hunky-dory on Windows, but on Linux the files get created, but are all empty.

The following small program shows exactly the same behaviour:

package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {
    f, err := os.OpenFile("test.txt", os.O_APPEND|os.O_CREATE, 0777)
    if err != nil {
        fmt.Println(err.Error())
        return
    }
    defer f.Close()

    w := bufio.NewWriter(f)
    _, err = w.Write([]byte("hello"))
    if err != nil {
        fmt.Println(err.Error())
    }
    w.Flush()
}

When run, the above code does not seem to output any errors on Linux. As can be seen by the file size of test.txt, it does write content to the file on Windows, while it does not do so on Linux.

Directory on Windows:

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       14.04.2016     10:37            345 main.go
-a----       14.04.2016     10:45             10 test.txt
-a----       14.04.2016     10:37        2635264 writetest.exe

Directory on Linux:

drwxrwxr-x 2 localuser localuser 4096 Apr 14 10:55 ./
drwxr-xr-x 8 localuser localuser 4096 Apr 14 10:27 ../
-rw-rw-r-- 1 localuser localuser  345 Apr 14 10:37 main.go
-rwxrwxr-x 1 localuser localuser    0 Apr 14 10:55 test.txt*

What am I missing here?

Change your flag, from os.O_APPEND|os.O_CREATE to os.O_RDWR|os.O_APPEND|os.O_CREATE will work on Linux and Mac OSX.

The key idea is event you want to append the file, you still need to open with Write flag in Linux and Mac OSX.