I am trying to rebuild a tee-like util by go language on Windows. But I found the encoding of the output is not always the same.
To make the problem simple, I wrote this program:
package main
import (
"fmt"
"io"
"os"
)
func main() {
count, err := io.Copy(os.Stdout, os.Stdin)
fmt.Println(count, err)
}
I named it test. In the Windows command console, I got these output:
>test
中
中
5 <nil>
It works fine with no pipe and redirect.
>echo 中 | test
��
5 <nil>
The output is collapsed if I get stdin from a pipe.
>echo 中 | test > test.txt
>type test.txt
中
5 <nil>
It works again when I redirect the output to a file.
>test > test.txt
中
>type test.txt
荳ュ
5 <nil>
But not work when I use the normal stdin and redirect to a file.
If I open this test.txt here by other editors like notepad++, I found it is encoded in UTF-8 and the content is 中
.
If I use Cygwin with a UTF-8 encoded console on Windows, everything is just good.
From the output, I know that the number of bytes the program copied is 5, which means it is using UTF-8 in the program no matter what the stdin is. But as I know the windows command line console is basically use non-unicode encoding, why it is converted into UTF-8? And is there a way to let the program just copy what the stdin send without any converting?
btw. If I use tee from gnuWin32 to do the same test, everything just works good.
>where tee
D:\Tools\gnuWin32\bin\tee.exe
>echo 中 | tee
中
>tee tee.txt
中
中
^C
>type tee.txt
中
Is there anyone know the reason of this and what is the solution?