Welcome To Golang By Example

channel as function argument in Go (Golang)

Overview

There are many ways in which a channel can be passed as a function argument. The direction of the arrow for a channel specifies the direction of flow of data

Bidirectional channel

Signature of such a bidirectional channel will be like below when passed to a function as an argument.

func process(ch chan int){ //doSomething }

Code:

package main
import "fmt"
func main() {
    ch := make(chan int, 3)
    process(ch)
}
func process(ch chan int) {
    ch <- 2
    s := <-ch
    fmt.Println(s)
}

Output: 2

Only Send Channel

func process(ch chan<- int){ //doSomething }
invalid operation: <-ch (receive from send-only type chan<- int)

Try uncommenting below line in the code to see the above error

s := <-ch

Code:

package main
import "fmt"
func main() {
    ch := make(chan int, 3)
    process(ch)
    fmt.Println(<-ch)
}
func process(ch chan<- int) {
    ch <- 2
    //s := <-ch
}

Output: 2

Only Receive Channel

func process(ch <-chan int){ //doSomething }
invalid operation: ch <- 2 (send to receive-only type <-chan int)

Try uncommenting below line in the code to see the above error

ch <- 2

Code:

package main
import "fmt"
func main() {
    ch := make(chan int, 3)
    ch <- 2
    process(ch)
    fmt.Println()
}
func process(ch <-chan int) {
    s := <-ch
    fmt.Println(s)
    //ch <- 2
}

Output: 2

Channel Pointer

This way of passing a channel would only make sense if you would want to modify the channel. This is very uncommon and not a preferable way to use. Signature of the such a channel which is passed as a pointer

func process(ch *chan int){ //doSomething}

Code:

package main

import "fmt"

func main() {
	ch := make(chan int, 3)
	process(&ch)
	fmt.Println(ch)
}

func process(ch *chan int) {
	*ch <- 2
	s := <-*ch
	*ch = nil
	fmt.Println(s)
}

Output: 2