On Wed, 25 Oct 2017 06:30:09 -0700 (PDT)
desaiabhi...@gmail.com wrote:

> I am expecting to show url1 (2 sec ), url2 (4 sec ) but not url3( 6
> sec) where timeout is 5 sec
> 
> but program showing only url1 value
> 
> Please help
> 
> https://play.golang.org/p/aMOoSEHjmZ
> 
> Thanks in advance
> 
> Rgds,
> 
> Abhi
> 
> 
> package main
> 
> import "fmt"
> import "time"
> 
> func InvokeUrl1(u string, val chan<- string) {
>     time.Sleep(2 * time.Second)
>     val <- "Url1 Value"
> }
> func InvokeUrl2(u string, val chan<- string) {
>     time.Sleep(4 * time.Second)
>     val <- "Url2 Value"
> }
> func InvokeUrl3(u string, val chan<- string) {
>     time.Sleep(6 * time.Second)
>     val <- "Url3 Value"
> }
> 
> func GetUrlValues(urls []string) {
> 
>     ch := make(chan string, 1)
>     for _, url := range urls {
>         go func(u string) {
>             val := make(chan string)
>             if u == "url1" {
>               go InvokeUrl1(u, val)
>             } else if u == "url2" {
>               go InvokeUrl2(u, val)
>             } else if u == "url3" {
>               go InvokeUrl3(u, val)
>             }
> 
>             select {
>             case ret := <-val:
>                 ch <- ret
>             case <-time.After(5 * time.Second):
>                 ch <- "nil"
>             }
>         }(url)
>     }
>     fmt.Println(<-ch)

You only print once, it suppose to be inside the loop or wrapped with
select while <-ch != "nil".

> }
> func main() {
>   GetUrlValues([]string{"url1", "url2", "url3"})
> }
> 



-- 
{ "name":"Mhd Sulhan", "phone":"+628567826625", "site":"kilabit.info" }

-- 
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