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.

Reply via email to