Not quite what you asked for but rather than use N goroutines, why not slurp in 
the whole file in a list  ([]string) of lines and then output them in random 
order? Something like

for _, i := range deal(len(lines)) {
  fmt.Println(lines[i])
}

Where you'd write deal(n) to return a list of n numbers in range 0..n-1 in a 
randomized order, without repeating a number.

> On Sep 20, 2016, at 3:43 AM, Unknown User <knowsuperunkn...@gmail.com> wrote:
> 
> Hi All,
> 
> I am trying to print the contents of  a file randomizing the lines.
> The following code works, but does not exit even after all the lines are read.
> What needs to be done to correct it?
> 
> package main
> 
> import(
>     "fmt"
>     "bufio"
>     "os"
>     "math/rand"
>     "time"
> )
> 
> func main() {
>     filename := os.Args[1]
>     if filename == "" {
>         panic("No filename")
>     }
>     fh,err := os.Open(filename)
>     if err != nil { 
>         panic(err)
>     }
>     scanner := bufio.NewScanner(fh)
>     c := make(chan string)
>     for scanner.Scan() {
>         line := scanner.Text()
>         err := scanner.Err()
>         if err != nil { 
>             close(c)
>         }
>         go func(l string){
>             time.Sleep(time.Duration(rand.Intn(30)) * time.Millisecond)
>             c <-  l
>         }(line)
>     }
>     for  { 
>         select { 
>             case myline := <- c:
>                 fmt.Println(myline)
>             default:
>                 break
>         }
>     }
> }
>  
> -- 
> You received this message because you are subscribed to the Google Groups 
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to