Channels and data communication

Go is a statically typed language, and this means that a given channel can only send or receive data of a single data type. In Go's terminology, this is known as a channel's element type. A Go channel will accept any valid Go data type including functions. Here is an example of a simple program that accepts and calls on functions:

// elems.go 
package main 
 
import "fmt" 
 
func main() { 
    // Let's create three simple functions that take an int argument 
    fcn1 := func(i int) { 
        fmt.Println("fcn1", i) 
    } 
    fcn2 := func(i int) { 
        fmt.Println("fcn2", i*2) 
    } 
    fcn3 := func(i int) { 
        fmt.Println("fcn3", i*3) 
    } 
 
    ch := make(chan func(int)) // Channel that sends & receives functions that take an int argument 
    done := make(chan bool)    // A Channel whose element type is a boolean value. 
 
    // Launch a goroutine to work with the channels ch & done. 
    go func() { 
        // We accept all incoming functions on Channel ch and call the functions with value 10. 
        for fcn := range ch { 
            fcn(10) 
        } 
        // Once the loop terminates, we print Exiting and send true to done Channel. 
        fmt.Println("Exiting") 
        done <- true 
    }() 
 
    // Sending functions to channel ch 
    ch <- fcn1 
    ch <- fcn2 
    ch <- fcn3 
 
    // Close the channel once we are done sending it data. 
    close(ch) 
 
    // Wait on the launched goroutine to end. 
    <-done 
} 

The output of the preceding code is as follows:

fcn1 10
fcn2 20
fcn3 30
Exiting

In the preceding code sample, we say that the channel ch has the element type of func(int) and the channel done has the element type of bool. There are a lot more interesting details in the code, but we shall discuss them in the following sections.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset