Video Coming Soon...

Created by Zed A. Shaw Updated 2025-10-23 14:49:14

31: cat

This exercise is pending. Quick notes about this exercise:

The Code

View Source file go-coreutils/cat/main.go Only

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)
}

type Opts struct {
  Number bool
  Squeeze bool
  Filenames []string
}

func parse_opts() (Opts) {
  var opts Opts

  flag.BoolVar(&opts.Number, "n", false, "Number all nonempty output lines, starting with 1")
  flag.BoolVar(&opts.Squeeze, "s", false, "Suppress repeated adjacent blank lines")
  flag.Parse()

  if flag.NArg() < 1 {
    log.Fatal("USAGE: cat [-n] [-s] file0 [fileN]")
    os.Exit(1)
  }

  opts.Filenames = flag.Args()

  return opts
}

func main() {
  opts := parse_opts()

  for _, filename := range opts.Filenames {
    in_file, err := os.ReadFile(filename)

    if err != nil { Fail(err, "cannot open %s:", filename) }

    if(opts.Number) {
      count := 1
      for line := range strings.Lines(string(in_file)) {
        if opts.Squeeze && len(line) <= 1 {
          continue
        }

        fmt.Printf("%0.4d: %s", count, line)
        count++
      }
    } else {
      fmt.Print(string(in_file))
    }
  }
}
Back to Module Next Lesson

Register for Learn Go the Hard Way

Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.