<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<oembed>
  <author_name>castaneai</author_name>
  <author_url>https://blog.hatena.ne.jp/castaneai/</author_url>
  <blog_title>castaneaiのブログ</blog_title>
  <blog_url>https://castaneai.hatenablog.com/</blog_url>
  <categories>
    <anon>go</anon>
  </categories>
  <description>Goで非同期的な処理の中断を検知したい場合、次のように select と ctx.Done() を使って書くことが多い。 とても便利なパターンなのだが、いくつか使うときの注意点がある。 select { case &lt;-ctx.Done(): // done case &lt;-ch: // ... } selectによる受信のランダム性 channelに届いたタスクを順番に処理しつつ、キャンセル要求が来たら終了する簡単なプログラムを例にする。 コードにある通り次のような順序で実行される。 task1を送る キャンセル要求を送る task2を送る package main import ( &quot;cont…</description>
  <height>190</height>
  <html>&lt;iframe src=&quot;https://hatenablog-parts.com/embed?url=https%3A%2F%2Fcastaneai.hatenablog.com%2Fentry%2Fgo-select-ctx-done-tips&quot; title=&quot;Go: selectでctx.Done()を受信するときの注意点 - castaneaiのブログ&quot; class=&quot;embed-card embed-blogcard&quot; scrolling=&quot;no&quot; frameborder=&quot;0&quot; style=&quot;display: block; width: 100%; height: 190px; max-width: 500px; margin: 10px 0px;&quot;&gt;&lt;/iframe&gt;</html>
  <image_url></image_url>
  <provider_name>Hatena Blog</provider_name>
  <provider_url>https://hatena.blog</provider_url>
  <published>2021-07-29 19:06:52</published>
  <title>Go: selectでctx.Done()を受信するときの注意点</title>
  <type>rich</type>
  <url>https://castaneai.hatenablog.com/entry/go-select-ctx-done-tips</url>
  <version>1.0</version>
  <width>100%</width>
</oembed>
