diff --git a/goblin.go b/goblin.go index f56249b..48c6fe4 100644 --- a/goblin.go +++ b/goblin.go @@ -14,6 +14,7 @@ import( "github.com/jienfak/goblin/ls" "github.com/jienfak/goblin/yes" "github.com/jienfak/goblin/date" + "github.com/jienfak/goblin/uniq" ) func main() { @@ -34,6 +35,7 @@ func main() { "ls" : ls.Run, "yes" : yes.Run, "date" : date.Run, + "uniq" : uniq.Run, } if binBase := path.Base(os.Args[0]) ; binBase != "goblin" { diff --git a/uniq/uniq.go b/uniq/uniq.go new file mode 100644 index 0000000..89b8b47 --- /dev/null +++ b/uniq/uniq.go @@ -0,0 +1,45 @@ +/* Yes program implementation. */ +package uniq + +import( + "os" + "fmt" + "flag" + "bufio" + "io" +) + +func Run(args []string) int { + var( + Uflag bool + ) + status := 0 + arg0 := args[0] + args = args[1:] + flagSet := flag.NewFlagSet(arg0, flag.ExitOnError) + flagSet.BoolVar(&Uflag, "U", false, "Print every line just one time.") + flagSet.Usage = func() { + fmt.Fprintf(os.Stderr, "Usage of %s: %s [options] [string]\n", arg0, arg0) + flagSet.PrintDefaults() + } + flagSet.Parse(args) + args = flagSet.Args() + + r := bufio.NewReader(os.Stdin) + u := make(map[string]int) + if Uflag { + for{ + l, e := r.ReadString('\n') + if e==io.EOF { + break + } + _, haskey := u[l] + if !haskey { + u[l] = 1 + fmt.Print(l) + } + } + } + + return status +}