Skip to content

Commit

Permalink
add file io sample
Browse files Browse the repository at this point in the history
  • Loading branch information
kicool committed Mar 19, 2012
1 parent caac07e commit dc0a4e1
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions io/io.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"bufio"
"io"
"os"
)

func main() {
fi, err := os.Open("input.txt")
if err != nil {
panic(err)
}
defer fi.Close()
r := bufio.NewReader(fi)

fo, err := os.Create("output.txt")
if err != nil {
panic(err)
}
defer fo.Close()
w := bufio.NewWriter(fo)

buf := make([]byte, 1024)
for {
n, err := r.Read(buf)
if err != nil && err != io.EOF {
panic(err)
}
if n == 0 {
break
}

if n2, err := w.Write(buf[:n]); err != nil {
panic(err)
} else if n2 != n {
panic("error in writing")
}
}

if err = w.Flush(); err != nil {
panic(err)
}
}

0 comments on commit dc0a4e1

Please sign in to comment.