Skip to content

Instantly share code, notes, and snippets.

@toshi0383
Created February 17, 2019 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toshi0383/5e243ed2091ffe3d3ef323ca7c0959e7 to your computer and use it in GitHub Desktop.
Save toshi0383/5e243ed2091ffe3d3ef323ca7c0959e7 to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"fmt"
"log"
"os"
)
func main() {
argsWithoutProg := os.Args[1:]
if len(argsWithoutProg) == 0 {
s := bufio.NewScanner(os.Stdin)
for s.Scan() {
fmt.Printf("%s\n", s.Text())
}
} else {
for _, path := range argsWithoutProg {
f, err := os.Open(path)
if err != nil {
os.Stderr.WriteString(fmt.Sprintf("cat: failed to open with err: %v", err))
continue
}
if isDir(path) {
os.Stderr.WriteString(fmt.Sprintf("cat: %s: Is a directory", path))
continue
}
s := bufio.NewScanner(f)
for s.Scan() {
fmt.Printf("%s\n", s.Text())
}
}
}
}
func isDir(path string) bool {
info, err := os.Stat(path)
if err != nil {
log.Panic("failed to Stat")
}
return info.Mode().IsDir()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment