This is an education project that attempts to reimplement the GNU coreutils in Go. You can find the full manual here: https://www.gnu.org/software/coreutils/manual/coreutils.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-coreutils/cat/main.go

43 lines
877 B

package main
import (
"fmt"
"flag"
"os"
"log"
"strings"
)
func Fail(err error, format string, v ...any) {
err_format := fmt.Sprintf("ERROR: %v; %s", err, format)
log.Printf(err_format, v...)
os.Exit(1)
}
func main() {
var number bool
flag.BoolVar(&number, "n", false, "Number all nonempty output lines, starting with 1")
flag.BoolVar(&number, "number", false, "Number all nonempty output lines, starting with 1")
flag.Parse()
if flag.NArg() < 1 {
log.Fatal("USAGE: cat [-n/--number] file0 [fileN]")
os.Exit(1)
}
filename := flag.Arg(0)
in_file, err := os.ReadFile(filename)
if err != nil { Fail(err, "cannot open %s:", filename) }
if(number) {
count := 1
for line := range strings.Lines(string(in_file)) {
fmt.Printf("%0.4d: %s", count, line)
count++
}
} else {
fmt.Print(string(in_file))
}
}