Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Send and receive on a nil channel in Go (Golang)

Posted on August 14, 2023August 14, 2023 by admin

Table of Contents

  • Overview
  • Code

Overview

The zero value of the channel is nil.Hence only declaring a channel creates a nil channel as default zero value of the channel is nil.Below is the result of send and receive operation on a nil channel

  •  Sending to a  nil channel blocks forever
  •  Receiving from nil channel blocks forever

Let’s see a program for it

Code

package main
import (
"fmt"
"time"
)
func main() {
var ch chan int
go send(ch)
<-ch
time.Sleep(time.Second * 1)
}
func send(ch chan int) {
fmt.Println("Sending value to channnel start")
ch <- 1
fmt.Println("Sending value to channnel finish")
}

Output

Sending value to channnel start
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan receive (nil chan)]:
goroutine 18 [chan send (nil chan)]:

In the above program, we are only declaring the channel hence a nil Channel was created since the default 0 value of the channel is nil.  After that, we sent to the channel in send function and received from the channel in the main function.  It results in a deadlock as sending to and receiving from a nil channel block forever.  That is why it  gives below output

  • go
  • golang
  • Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2023 Welcome To Golang By Example | Design: Web XP