Sorry again.   Well, I followed your advice. Here is the link 
https://play.golang.org/p/7DigEVsKbdx
How can I cancel goToSleep() when timeout is reached?


El lunes, 24 de febrero de 2020, 14:16:32 (UTC-3), Jake Montgomery escribió:
>
> Your code will still not compile. In this group, it is often helpful to 
> include a link to your code in the playground (https://play.golang.org/) 
> using the "Share" button. That is in addition to, or instead of, posting 
> your code in the message. This will allow others to easily run your code, 
> but also will allow you to make sure it compiles. 
>
> On Monday, February 24, 2020 at 10:01:01 AM UTC-5, Juan Mamani wrote:
>>
>> Sorry}, here is the right code:  ( just was the effect to b working til 
>> At 5:20am  sleepy and lost)
>>
>> Expected behaviour is  to end  anonyimous func when  3s timeout is 
>> reached.
>>
>> //------------------------------------------------
>> package main
>>
>> import(
>> //"fmt"
>> "time"
>> "math/rand"
>> "log"
>> )
>>
>> func main(){
>>
>>  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
>>             }
>>         }
>>     }()
>>     return terminated
>> }
>>
>> 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.")
>> }
>>
>> 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(12) // n will be between 0 and 10
>>     log.Println("Sleeping ",n,"seconds..")
>>     time.Sleep(time.Duration(n)*time.Second)
>>     log.Println("Done sleeping!")
>> }
>>
>> //------------------------------------------------
>>
>>
>>
>> El lunes, 24 de febrero de 2020, 6:09:09 (UTC-3), Lutz Horn escribió:
>>>
>>> > 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 are you trying to do? What is the expected behaviour, what happens 
>>> instead? 
>>>
>>> BTW, this code does not compile, there are same unbalanced curly 
>>> brackets. 
>>>
>>> Lutz 
>>>
>>

-- 
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/b0828575-fddf-4024-bec0-8faf5b9858a4%40googlegroups.com.

Reply via email to