diff options
author | Silvan Jegen <s.jegen@gmail.com> | 2015-10-23 18:53:25 +0200 |
---|---|---|
committer | Silvan Jegen <s.jegen@gmail.com> | 2015-10-27 21:40:57 +0100 |
commit | a29104ce4bbe4afc2aa1cca3b941747237a70c49 (patch) | |
tree | 2d9f91ef3353884030e7614c4afd15977e24aeee | |
parent | 6993463ee17b2c6b2de65649d535e878e3310827 (diff) |
Fix tabwriter issue
There is a race condition now.
-rw-r--r-- | gwic.go | 19 |
1 files changed, 12 insertions, 7 deletions
@@ -75,17 +75,17 @@ func feedworkers(wlist []*work, w string) []*work { return ret } -func print_kwic(sl []string, c chan string) { +func print_kwic(sl []string, c chan string, out chan string) { for w := range c { sl = append(sl, w) } - fmt.Fprintf(tw, "%s\n", strings.Join(sl, "\t")) + out <- strings.Join(sl, "\t") } -func printqueue(q *queue, c chan string, left int) { +func printqueue(q *queue, words chan string, left int, out chan string) { var hungryhippos []*work - for w := range c { + for w := range words { index := strings.Index(w, kw) if index < 0 { q.insert(w) @@ -108,11 +108,12 @@ func printqueue(q *queue, c chan string, left int) { //fmt.Fprintf(os.Stdout, "copied %d : %v\n", n, sl) hungryhippos = append(hungryhippos, wc) //fmt.Fprintf(os.Stderr, "appended hungryhippos: %v %d\n", hungryhippos, len(hungryhippos)) - go print_kwic(sl, wc.c) + go print_kwic(sl, wc.c, out) } for _, wc := range hungryhippos { close(wc.c) } + close(out) } var ( @@ -134,6 +135,7 @@ func main() { q := queue{maxl: MAX, q: make([]string, MAX)} sc := make(chan string, 1000) + out := make(chan string, 1000) reader := bufio.NewReader(os.Stdin) line, err := reader.ReadString(byte('\n')) @@ -151,7 +153,10 @@ func main() { close(c) }(sc) - printqueue(&q, sc, 5) - fmt.Printf("Reached end\n") + go printqueue(&q, sc, 5, out) + + for l := range out { + fmt.Fprintf(tw, "%s\n", l) + } tw.Flush() } |