Golang program to creates a unidirectional sending channel and passes it to a function that takes a pointer to a slice of integers


There are many cases while working on go language channels where you need to write a program that creates a unidirectional sending channel and passes it to a function that takes a pointer to a slice of integers for data streaming, asynchronous data sharing and more, in this go language article we will write such a program using the make() function as well as using channel types.

Syntax

 ch := make(chan<- string)

To declare a send only channel named “ch”.

go sendOnlyChannel(ch, &wg) 

To launch a new goroutine we use the “go” keyword. Here the go keyword is executing the SendOnlyChannel() function.

var wg sync.WaitGroup

Sync.WaitGroup, is defined as a variable and is used to synchronize the goroutines.

Algorithm

  • Define the sendData function, which accepts as parameters a unidirectional transmitting channel ch of type chan- int and a slice of integers data.

  • Iterate through each val value in the data slice: Send the channel "ch" the value val.

  • To indicate that no additional values will be sent, close the channel "ch".

  • Determine the primary function. Make an integer data slice with the values 1, 2, 3, 4, 5.

  • Using the make() method, create a bidirectional channel of type “chan int " called "ch".

  • Call the sendData method, supplying the channel ch and the data slice as parameters, to start a goroutine.

  • Enter a loop to receive the values sent through the channel. Print the "val."

Example 1

The code given below creates a channel and starts a separate goroutine to send the values from a slice to a channel, and the other goroutine prints the value. This code is an example of concurrent communication between channels in go language.

package main
import "fmt"
func sendData(ch chan<- int, data []int) {
   for _, val := range data {
      ch <- val
   }
   close(ch)
}
func main() {
   data := []int{1, 2, 3, 4, 5}
   ch := make(chan int)
   go sendData(ch, data)
   for val := range ch {
      fmt.Println(val)
   }
}

Output

1
2
3
4
5

Example 2

This code is an example of concurrent communication between channels in go language. This code introduces the done channel, the done channel is used as a signal of completion, it allows the main goroutine to wait before the sending channel completes the execution.

package main
import (
   "fmt"
)
func sendData(ch chan<- int, data *[]int, done chan<- bool) {
   for _, val := range *data {
      ch <- val
}
close(ch)
done <- true
}
func main() {
   data := []int{1, 2, 3, 4, 5}
   ch := make(chan int)
   done := make(chan bool)
   go sendData(ch, &data, done)
   for val := range ch {
      fmt.Println(val)
   }
   // Waiting to receive a value indicating completion
   <-done
}

Output

1
2
3
4
5

Conclusion

In this article we looked at two approaches for constructing unidirectional transmitting channels in Go Language and passing it to the function that takes a pointer to the slice of an integer. The first technique used the make() function, whereas the second method used the arrow notation. The make function allocates and initializes the memory correctly and the arrow notion enables quick communication between goroutines.

Updated on: 13-Jul-2023

58 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements