0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

go チャネルから送る値がなくなっても チャネルから値を繰り返し受信し続けることによるエラーの確認。

Posted at

closeしない場合のエラー内容確認


package main

import "fmt"

func routine1(int_slice []int, channel_kun chan int) {
	sum := 0
	for _, v := range int_slice {
		sum += v
		channel_kun <- sum
	}
	// close(channel_kun)
}

func main() {
	int_slice := []int{1, 2, 3, 4, 5}
	channel_kun := make(chan int, len(int_slice))
	go routine1(int_slice, channel_kun)
	for i := range channel_kun {
		fmt.Println(i)
	}
}


実行結果


1
3
6
10
15
fatal error: all goroutines are asleep - deadlock!

goroutine 1 [chan receive]:
main.main()
        C:/Users/addre/dev/go_channel_close/main.go:18 +0x145
exit status 2

結論

分かりずらい。
とりあえずrangeの行でエラーが出ている時は、channelがcloseしてないということを疑えば良いかな...

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?