fprintln()在Windows下编写Linux样式的行尾

I'm running Go on Windows and writing lines to a file with fmt.Fprintln(w, line), but the end of line is Linux style end of lines and not Windows. Is there an environment variable I need to set or something?

No, fmt always uses unix line endings. If you want something different, you need to print it yourself.

As Stephen Weinberg already said, the print function in fmt always use as line separator.

You can see this in the code here.

As a workaround you may define your own print function or use Printf instead of Println:

fmt.Printf("Foo
")

If you want to define a global string for the newline used in the environment your program runs, you may create a .go file for each operating system and use build tags to select the right one.

Alternatively, write a function which parses runtime.GOOS (play):

func Newline() string {
    switch runtime.GOOS {
    case "windows":
        return "
"
    // ...more...
    case "linux":
        fallthrough
    default:
        return "
"
    }
}

fmt.Printf("This is a line%s", Newline())