-1

I just started using go and wrote my first program but the output is not as expected. I have writtern a async routine addUrl which adds url to channel 5000 times and consumeUrl removes from the channel and prints it. The routine runs only 9 time. Why is it? Below is the code and output

package main

import "fmt"
import "time"

var urlCount = 0

func main(){

    urlHolder := make(chan string,5000)

    fmt.Printf("Starting program")

    go addUrls(urlHolder)

    time.Sleep(time.Millisecond * 100)
    go consumeUrls(urlHolder)

    fmt.Printf("Done")

}

func addUrls(urlHolder chan string){
    var myurl string = "https://example.com/"

    for i:=0; i<5000 ; i++ {
        urlHolder<-myurl
        fmt.Printf(" %d url added \n",i)
        time.Sleep(time.Millisecond * 10)
    }

}

func consumeUrls(urlHolder chan string) {
    urlCount++
    urlsConsumed := <- urlHolder
    fmt.Printf("Pulled url %d",urlCount," ",urlsConsumed,"\n")
    time.Sleep(time.Millisecond * 20)
}  

The output is

Starting program
 0 url added 
 1 url added 
 2 url added 
 3 url added 
 4 url added 
 5 url added 
 6 url added 
 7 url added 
 8 url added 
Done

Why is it terminating at 8 when loop is 5000?

TestingInProd
  • 349
  • 10
  • 26
  • Most probably because the main thread exits before the goroutine got time to complete its work. Put a sleep of 2/3 seconds at the end of main function and check. Also remove the sleep from addUrls routine. – Nipun Talukdar Oct 29 '16 at 03:17
  • Hey thanks ..That was the problem .. – TestingInProd Oct 29 '16 at 03:26
  • 1
    Possible duplicate of [What's wrong with this golang code?](http://stackoverflow.com/questions/28958192/whats-wrong-with-this-golang-code); and [Goroutine does not execute if time.Sleep included](http://stackoverflow.com/questions/28307783/goroutine-does-not-execute-if-time-sleep-included). – icza Oct 29 '16 at 09:51
  • Using a [`sync.WaitGroup`](https://golang.org/pkg/sync/#WaitGroup) or reading on a channel in `main` will be more fault tolerant and responsive than a sleep when taking into account any variance in the goro execution. Sleep is a pretty error prone synchronisation mechanism. – nothingmuch Oct 29 '16 at 15:19
  • @nothingmuch Yuval? Welcome! – hobbs Oct 31 '16 at 06:00
  • @hobbs Indeed! Hi! Thanks! – nothingmuch Oct 31 '16 at 08:58

2 Answers2

1

You are using time.Sleep to wait for main to finish but you really should be using WaitGroups.

That way you don't have to try to pick some arbitrary time and hope it's enough for your program to finish, or worry about setting too much time and your program sits around doing nothing.

I've added the implementation of WaitGroups to your code here:

https://play.golang.org/p/1zn2JYefaA

Also, the way your consumeUrls function is written is not looping properly and you won't get everything in your channel printed. But since that wasn't your specific question I won't address it here.

nosequeldeebee
  • 935
  • 9
  • 14
-1

Actually The problem was like Nipun said, the main got terminated early.

TestingInProd
  • 349
  • 10
  • 26