From a29104ce4bbe4afc2aa1cca3b941747237a70c49 Mon Sep 17 00:00:00 2001 From: Silvan Jegen Date: Fri, 23 Oct 2015 18:53:25 +0200 Subject: Fix tabwriter issue There is a race condition now. --- gwic.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/gwic.go b/gwic.go index b71a307..98ee34f 100644 --- a/gwic.go +++ b/gwic.go @@ -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() } -- cgit v1.2.3