Am Sonntag, 6. August 2017 09:53:36 UTC+2 schrieb Abhijit Desai: > > Can you please help with below code to get output at specific cutoff time > and exit > > Thanks in advance > > Abhi > > > > package main > > import "time" > import "fmt" > > func main() { > > c1 := make(chan string) > > go func() { //Sending data after certain time > > c1 <- "result 1" > > time.Sleep(time.Second * 1) > c1 <- "result 2 afer 1 sec" > > time.Sleep(time.Second * 1) > c1 <- "result 2 afer 2 sec" > > time.Sleep(time.Second * 1) > c1 <- "result 2 afer 3 sec" > > time.Sleep(time.Second * 1) > c1 <- "result 2 afer 4 sec" > > time.Sleep(time.Second * 1) > c1 <- "result 2 afer 5 sec" > }() > > select { > case <-time.After(time.Second * 4): { //cut off 4s and return the > value > res := <-c1 > fmt.Println(res) // expecting result "result 2 afer 3 sec" > but returning "result 1" >
Why would you expect this? "result 1" is the fist value that was sent to the channel. Channels are sort of FIFO. > } > } > } > -- 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.