Closing channel of unknown length

前端 未结 2 1646
孤街浪徒
孤街浪徒 2020-12-16 08:20

I\'m not able to close channel when there is no knowledge about its
length

package main

import (
    \"fmt\"
    \"time\"
)

func gen(ch chan int) {
            


        
2条回答
  •  既然无缘
    2020-12-16 09:02

    "One general principle of using Go channels is don't close a channel from the receiver side and don't close a channel if the channel has multiple concurrent senders."

    Every channel will be GCed eventually once it is marked for cleanup, so it is okay to leave channel un-closed the only difference it will make is that that channel will be available for gc after a few cycles maybe if not closed explicitly.

    Nevertheless it is always good if you can close the channel off. Please go through the following links for detailed explaination.

    Articles this and this shows various ways to close a channel in case of 1:N, N:1 or M:N (senders:receivers)

提交回复
热议问题