On Fri, Jun 21, 2013 at 12:11 PM, <
jord...@gmail.com> wrote:
>
> I'm evaluating Go as a substitute to Python for a task I need to do.
> I'm reading from STDIN and saving the information to a file.
> The following code takes around 3 secs to read 1million lines from stdin and
> writing them to a file using Go1.1 (OS Debian Linux)
>
> package main
> import "fmt"
> import "bufio"
> import "os"
>
> func main() {
> f, _ := os.Create("outputgo.txt")
> reader := bufio.NewReader(os.Stdin)
> for {
> line, err := reader.ReadString('\n')
> if err != nil {
> fmt.Println("%s", line)
> return
> }
> f.WriteString(line)
> }
> }
This code is going to do a lot of copying between []byte and string.
One thing to learn about Go is that when doing I/O, stick to []byte.
This is also, of course, an implausible way to copy a file, but
presumably you really do want to read lines for some reason rather
than simply reading buffers of data. To read lines in Go 1.1, you
should bufio.NewScanner. See
http://golang.org/pkg/bufio/#example_Scanner_lines .
Ian