mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2024-11-14 02:13:51 +00:00
34 lines
588 B
Go
34 lines
588 B
Go
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
|
|
// See page 96.
|
|
|
|
// Dedup prints only one instance of each line; duplicates are removed.
|
|
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"os"
|
|
)
|
|
|
|
//!+
|
|
func main() {
|
|
seen := make(map[string]bool) // a set of strings
|
|
input := bufio.NewScanner(os.Stdin)
|
|
for input.Scan() {
|
|
line := input.Text()
|
|
if !seen[line] {
|
|
seen[line] = true
|
|
fmt.Println(line)
|
|
}
|
|
}
|
|
|
|
if err := input.Err(); err != nil {
|
|
fmt.Fprintf(os.Stderr, "dedup: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
//!-
|