Skip to content
Snippets Groups Projects
Commit 594b9aeb authored by Marcus Schiesser's avatar Marcus Schiesser
Browse files

added gocat implementation

parents
No related branches found
No related tags found
No related merge requests found
package main
import (
"fmt"
"io"
"os"
)
const BUFFERSIZE = 1024
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: gocat [file ...]")
}
files := os.Args[1:]
for _, file := range files {
if err := processFile(file); err != nil {
fmt.Fprintf(os.Stderr, "gocat: %v", err)
}
}
}
func processFile(filename string) error {
var f *os.File
if filename == "-" {
f = os.Stdin
} else {
var err error
// f, err := doesn't work as this would keep the f scope local
f, err = os.Open(filename)
if err != nil {
return err
}
defer f.Close()
}
if err := bufferedCopy(f, os.Stdout); err != nil {
return err
}
return nil
}
func bufferedCopy(source io.Reader, destination io.Writer) error {
buf := make([]byte, BUFFERSIZE)
for {
n, err := source.Read(buf)
if err != nil && err != io.EOF {
return err
}
if n == 0 {
break
}
if _, err := destination.Write(buf[:n]); err != nil {
return err
}
}
return nil
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment