An overview of the
You can also create a fragment or array of channel data types in Golang. In fact, fragments or arrays of any data type can be created in Go. This tutorial includes simple examples of creating channel shards or arrays in Golang.
It should be added here that arrays are fixed size in Golang, while fragments can have variable sizes.
More details here
Array – https://golangbyexample.com/understanding-array-golang-complete-guide/
Slice – https://golangbyexample.com/slice-in-golang/
Segment of channel
package main
import "fmt"
func main(a) {
channel1 := make(chan int)
channel2 := make(chan int)
channel3 := make(chan int)
//First Way
var channels_first []chan int
channels_first = append(channels_first, channel1)
channels_first = append(channels_first, channel2)
channels_first = append(channels_first, channel3)
fmt.Println("\nOutput for First slice of channels")
for _, c := range channels_first {
fmt.Println(c)
}
//Second Way
channels_second := make([]chan int.3)
channels_second[0] = channel1
channels_second[1] = channel2
channels_second[2] = channel3
fmt.Println("\nOutput for Second slice of channels")
for _, c := range channels_second {
fmt.Println(c)
}
}
Copy the code
The output
Output for First slice of channels
0xc000118000
0xc000118060
0xc0001180c0
Output for Second slice of channels
0xc000118000
0xc000118060
0xc0001180c0
Copy the code
The output on your machine will be different because it is an address.
In the above program, we created three channels with data type int.
channel1 := make(chan int)
channel2 := make(chan int)
channel3 := make(chan int)
Copy the code
So there are two ways to create a channel slice. The first way is
var channels_first []chan int
channels_first = append(channels_first, channel1)
channels_first = append(channels_first, channel2)
channels_first = append(channels_first, channel3)
Copy the code
Second, we use the make command to create a channel slice.
channels_second := make([]chan int.3)
channels_second[0] = channel1
channels_second[1] = channel2
channels_second[2] = channel3
Copy the code
Either way it works. So that’s how we create a channel slice, right
Array of channels
package main
import "fmt"
func main(a) {
channel1 := make(chan int)
channel2 := make(chan int)
channel3 := make(chan int)
var channels_first [3]chan int
channels_first[0] = channel1
channels_first[1] = channel2
channels_first[2] = channel3
fmt.Println("Output for First Array of channels")
for _, c := range channels_first {
fmt.Println(c)
}
channel_second := [3]chan int{
channel1,
channel2,
channel3,
}
fmt.Println("\nOutput for Second Array of channels")
for _, c := range channel_second {
fmt.Println(c)
}
}
Copy the code
The output
Output for First Array of channels
0xc00008c060
0xc00008c0c0
0xc00008c120
Output for Second Array of channels
0xc00008c060
0xc00008c0c0
0xc00008c120
Copy the code
The output on your machine will be different because it is an address.
In the above program, we created three channels with data type int.
channel1 := make(chan int)
channel2 := make(chan int)
channel3 := make(chan int)
Copy the code
So we have two ways to create an array. The first way is
var channels_first [3]chan int
channels_first[0] = channel1
channels_first[1] = channel2
channels_first[2] = channel3
Copy the code
In the second way, we initialize the array directly with the created channel
channel_second := [3]chan int{
channel1,
channel2,
channel3,
}
Copy the code
Take a look at our advanced Golang tutorial. This series of tutorials is carefully designed and we try to cover all the concepts with examples. This tutorial is for those who wish to gain professional knowledge and a solid understanding of Golang – Golang Advanced Tutorial
If you are interested in learning how to implement all design patterns in Golang. If so, then this article is for you — All Design Patterns Golang
The postSlice or Array of Channels in Go (Golang)appeared first onWelcome To Golang By Example.