Created
February 17, 2019 05:57
-
-
Save toshi0383/5e243ed2091ffe3d3ef323ca7c0959e7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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