Skip to content
This repository was archived by the owner on Oct 7, 2018. It is now read-only.
Zhao Lu edited this page May 25, 2013 · 2 revisions

Implement unix utility echo, with the -n option.

package main

import (
  "os"
  "flag"
)

var omitNewline = flag.Bool("n", false, "don't print final newline")

const (
  Space = " "
  Newline = "\n"
)

func main() {
  flag.Parse() // Scans the arg list and sets up flags
  var s string = ""
  for i := 0; i < flag.NArg(); i++ {
    if i > 0 {
      s += Space
    }
    s += flag.Arg(i)
  }
  if !*omitNewline {
    s += Newline
  }
  os.Stdout.WriteString(s)
} 

Clone this wiki locally