Read-only Channels in Golang

Posted on 2021-10-25. Approx 200 words. Approx 1 minutes read.
Tags: #golang

In golang, channels can be defined as read-only, write-only or read-write.

This is an example of write-only channel:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
package main

import (
	"fmt"
	"math/rand"
)

func fillChannel(c chan<- int) {
	c <- rand.Intn(100)

	// Uncommenting this will result in compilation error:
	// invalid operation: cannot receive from send-only channel c (variable of type chan<- int)
	// _ = <-c
}

func main() {
	c := make(chan int)
	go fillChannel(c)
	x := <-c
	fmt.Println(x)
}

Similarly, example of read-only channel:

1
2
3
4
5
6
7
8
func consumer(c <-chan int) {
	myVal := <-c
	fmt.Println(myVal)

	// Uncommenting this will result in compilation error:
	// invalid operation: cannot send to receive-only type <-chan int
	// c <- 100
}

This can be useful in cases where we want to limit possible operation on channel passed to a function.