Hi everybody,

I've tried to adapt from from "Concurrency in Go by Katherine Cox-Buday" to 
understand how to apply timeout. But I don't get it.  What I'm doing 
wrong?     Here the code:

package main

import(
  //"fmt"
    "time"
    "math/rand"  
    "log"
)                                                                          
                                                                            
                

func main(){
  
 // infinity loop                                                          
                                                                            
                                 
  for{
       log.Println("-----------------Start")                                
                                                                            
                         
      Task()                                                                
                                                                            
                      
       log.Println("-----------------End")
  }
}
                                                                            
                                                                            
                                                                            
                                        // Trying to adapt from Concurrency 
in Go by Katherine Cox-Buday
func Task(){                                                                
                                                                            
                
   doWork := func(  done <-chan interface{}, strings <-chan string, ) 
<-chan interface{} { //1
      terminated := make(chan interface{})                                  
                                                                            
                      
     go func() {                                                            
                                                                            
                            
               defer log.Println("doWork exited.")                          
                                                                            
                              
               defer close(terminated)                                      
                                                                            
                              
               for {                                                        
                                                                            
                                   
                 goToSleep()                                                
                                                                            
                                 
                 select {
                      //case s := <-strings:
                      // case s := ran():
                      // Do something interesting                          
                                                                            
                                      
                      //fmt.Println(s)
                      case <-done: //2
                          return
            }
        }
      }()    
  }
                                                                            
                                                                            
                
  done := make(chan interface{})                                            
                                                                            
                  
  terminated := doWork(done, nil)                                          
                                                                            
                                                                            
                                                                            
                                   
  go func() { //3                                                          
                                                                            
                    
      // Cancel the operation after 3 second.                              
                                                                            
                       
     time.Sleep(3 * time.Second)                                            
                                                                            
                     
     log.Println("Canceling doWork goroutine reach 3s...")                  
                                                                            
                    
     close(done)                                                            
                                                                            
                 
  }()                                                                      
                                                                            
                                                                            
                                                                            
                                   
   <-terminated //4                                                        
                                                                            
                   
   log.Println("Done.")                                                    
                                                                            
                    }                                                      
                                
}                                                                          
                                                                            
                                                                            
                                                                            
                                                                            
                                                                            
                                                                            
  
 func goToSleep(){                                                          
                                                                            
                                           
     rand.Seed(time.Now().UnixNano())
     n := rand.Intn(10)
     log.Printf("Sleeping %d seconds....\n",n)
     time.Sleep(time.Duration(n)*time.Second)
     log.Println("Done sleeping!")
}                                                                          
                                    

//-------------------------------------

Thanks
JM


-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/f26a817e-4133-4f9e-b3cc-d83e3de5e7d9%40googlegroups.com.

Reply via email to