Skip to content

Instantly share code, notes, and snippets.

@qbig
Last active August 10, 2018 02:16
Show Gist options
  • Select an option

  • Save qbig/a6e40b74926c5ee20bfb70c72e3b4161 to your computer and use it in GitHub Desktop.

Select an option

Save qbig/a6e40b74926c5ee20bfb70c72e3b4161 to your computer and use it in GitHub Desktop.
Go Channel Axiom (Nil is nice, Close is nasty)

Channel Axioms

A send to a nil channel blocks forever A receive from a nil channel blocks forever

A send to a closed channel panics A receive from a closed channel returns the zero value immediately

Check if a channel is closed

// For Reader
v, ok := <- c
if ok {
  fmt.Println("not closed")
}

// For Writer? 
// You can't do anything.. :<
// But the rule of thumb is that only writer should close channel. So as writer, you shouldn't need to check if a channel is // closed ever as you should know the answer ..

Closing a closed channel will panic

	c := make(chan int)
	close(c)
	close(c) // panic!
	fmt.Println("Hello, playground", c)

Closing of a nil channel will panic

package main

import (
	"fmt"
)

func main() {
	var c chan int
	close(c) // panic!
	fmt.Println("Hello, playground", c)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment