Code Fix

中級send on closed channel パニックの原因と直し方 › パターン4

send on closed channel パニックの原因と直し方

close()で閉じた後のチャネルに値を送信しようとすると、実行時にパニックが発生します。

 1  package main
 2  
 3  func main() {
 4  	ch := make(chan int, 3)
 5  	close(ch)
 6  	_________
         ^
 7  }
panic: send on closed channel

空欄を埋めて実行を通す

広告
広告スロット(未設定)

send on closed channel パニックの原因と直し方

チャネルを閉じるのは送信側の役割で、閉じた後は一切送信できません。

このエラーの解説と類題をまとめて見る →

広告
広告スロット(未設定)