I run example B both playground and my linux(amd64) node, behavior is 
different

At playground, function A, B, C run immediately, then program end 
immediately
At my linux node, function A, B, C run immediately, but program end after 
wait 5 seconds

I confuse why function A and C never sleep 5 seconds during running 
playground ?  

On Saturday, January 21, 2017 at 11:34:09 AM UTC+8, Rodolfo Azevedo wrote:
>
> Hi all,
>
> I have a question, interesting:
>
> Why the executes is diferente in these both situations?
>
> Execute A:
>
> [11:19]  
>  ```package main
>
> import (
>     "fmt"
>     "sync"
>     "time"
> )
>
> var wg sync.WaitGroup
>
> func A() {
>
>     time.Sleep(time.Second * 5)
>     fmt.Println("Função A")
>     wg.Done()
>
> }
>
> func B() {
>
>     fmt.Println("Função B")
>     wg.Done()
>
> }
>
> func C() {
>
>     time.Sleep(time.Second * 5)
>     fmt.Println("Função C")
>     wg.Done()
>
> }
>
> func main() {
>
>     wg.Add(3)
>     defer wg.Wait()
>
>     go A()
>     go B()
>     go C()
>
> }
> ```
>
> Execute B:
>
> [11:20]  
>  ```package main
>
> import (
>     "fmt"
>     "sync"
>     "time"
> )
>
> var wg sync.WaitGroup
>
> func A() {
>
>     fmt.Println("Função A")
>     time.Sleep(time.Second * 5)
>     wg.Done()
>
> }
>
> func B() {
>
>     fmt.Println("Função B")
>     wg.Done()
>
> }
>
> func C() {
>
>     fmt.Println("Função C")
>     time.Sleep(time.Second * 5)
>     wg.Done()
>
> }
>
> func main() {
>
>     wg.Add(3)
>     defer wg.Wait()
>
>     go A()
>     go B()
>     go C()
>
> }
> ```
>
> [11:21]  
> In execute A, function A and C wait 5 seconds, then prints output. In 
> execute B no, all the prints executes without respecting time.Sleep. 
> Someone can explain why?
>
> [11:22]  
> I only change the order of time.Sleep in functions A and C.
>

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