tour icon indicating copy to clipboard operation
tour copied to clipboard

tour: [REPLACE WITH SHORT DESCRIPTION]

Open nerewarin opened this issue 2 years ago • 1 comments

Context: https://go.dev/tour/concurrency/6

Change the title above to describe your issue and add your feedback here, including code if necessary

Unclear description "The default case in a select is run if no other case is ready." but if we profile we this adding time we can see that both case1 and default are happens

package main

import (
	"fmt"
	"time"
)

func main() {
	tick := time.Tick(100 * time.Millisecond)
	boom := time.After(500 * time.Millisecond)
	for {
		n := time.Now()
		select {
		case <-tick:
			fmt.Println(n, "tick.")
		case <-boom:
			fmt.Println(n, "BOOM!")
			return
		default:
			fmt.Println(n, "    .")
			time.Sleep(50 * time.Millisecond)
		}
	}
}

2009-11-10 23:00:00 +0000 UTC m=+0.000000001 . 2009-11-10 23:00:00.05 +0000 UTC m=+0.050000001 . 2009-11-10 23:00:00.1 +0000 UTC m=+0.100000001 tick. 2009-11-10 23:00:00.1 +0000 UTC m=+0.100000001 . 2009-11-10 23:00:00.15 +0000 UTC m=+0.150000001 . 2009-11-10 23:00:00.2 +0000 UTC m=+0.200000001 tick. 2009-11-10 23:00:00.2 +0000 UTC m=+0.200000001 . 2009-11-10 23:00:00.25 +0000 UTC m=+0.250000001 . 2009-11-10 23:00:00.3 +0000 UTC m=+0.300000001 tick. 2009-11-10 23:00:00.3 +0000 UTC m=+0.300000001 . 2009-11-10 23:00:00.35 +0000 UTC m=+0.350000001 . 2009-11-10 23:00:00.4 +0000 UTC m=+0.400000001 tick. 2009-11-10 23:00:00.4 +0000 UTC m=+0.400000001 . 2009-11-10 23:00:00.45 +0000 UTC m=+0.450000001 . 2009-11-10 23:00:00.5 +0000 UTC m=+0.500000001 BOOM!

see output lines 3+4 - the time is the same, both case1 AND default happened. If both cases could happen - why we don't have tick (case1) before the last line with BOOM (case2)?

nerewarin avatar May 09 '22 22:05 nerewarin

See the page before were it says "It chooses one at random if multiple are ready. "

crisman avatar Mar 13 '23 01:03 crisman