Search code examples
gogoroutine

Why need to wait on select in Go?


I've just learning cancel on context. It's my code.

package main

import (
  "fmt"
  "context"
)

func main() {
  ctx := context.Background()

  do(ctx)
}

func do(ctx context.Context) {
  ctx, ctxCancel := context.WithCancel(ctx)

  resultCh := make(chan string)

  go terminate(ctx, resultCh)

  resultCh <- "value1"
  resultCh <- "value2"

  fmt.Println("pre cancel")

  ctxCancel()

  fmt.Println("post cancel")
}

func terminate(ctx context.Context, ch <-chan string) {
  for {
    select {
    case <-ctx.Done():
      fmt.Println("terminate")
      return
    case result := <-ch:
      fmt.Println(result)
    }
  }
}

want to ask

why that happened. what I have to need knowledgement?

I'm expect the output.

But got actual output didn't include "terminate".

value1
value2
pre cancel
terminate
post cancel

Fixed code

I added time.Sleep under the cancel func. Then output is my expected.

ctxCancel()
time.Sleep(100 * time.Millisecond) // add

Solution

  • From what i understand, the core idea behind using select is to wait until at least one of the cases becomes "ready." I included an example below that may help a bit. here the select is used to wait for either the value to be received from the channel ch or a timeout of 1 second to occur.

    import (
        "fmt"
        "time"
    )
    
    func main() {
        ch := make(chan int)
    
        go func() {
            // Simulate some time-consuming operation
            time.Sleep(2 * time.Second)
            ch <- 42
        }()
    
        select {
        case result := <-ch:
            fmt.Println("Received result:", result)
        case <-time.After(1 * time.Second):
            fmt.Println("Timeout reached")
        }
    }