Home > Backend Development > Golang > How can I create Go channels that can accept values of multiple different types?

How can I create Go channels that can accept values of multiple different types?

Linda Hamilton
Release: 2024-11-04 03:08:02
Original
868 people have browsed it

How can I create Go channels that can accept values of multiple different types?

Type-Agnostic Channels in Go

Channels in Go are a powerful communication mechanism that allows goroutines to send and receive data. In most cases, channels are used to convey values of a specific type. However, it is possible to create channels that can accept multiple different types.

Consider the following example:

<code class="go">package main

import (
    "fmt"
    "time"
)

func main() {
    ch := make(chan interface{})
    go func() {
        for {
            select {
            case p := <-ch:
                fmt.Printf("Received a %q\n", reflect.TypeOf(p).Name())
            case <-time.After(time.Second):
                fmt.Println("Empty Loop")
                return
            }
        }
    }()
    ch <- "Hello"
    time.Sleep(time.Second)
    ch <- 123
    time.Sleep(time.Second)
    ch <- struct{}{}
    time.Sleep(time.Second)
    ch <- []string{"Go", "programming"}
    time.Sleep(time.Second)
}</code>
Copy after login

This code creates a channel of type chan interface{} which can accept any type of value. When the goroutine receives a value on the channel, it uses reflection to determine the type of the value and prints it out.

You can also use a type switch to handle different types of values on the channel, as shown below:

<code class="go">package main

import (
    "fmt"
    "reflect"
    "time"
)

func main() {
    ch := make(chan interface{})
    go func() {
        for {
            select {
            case p := <-ch:
                switch p := p.(type) {
                case string:
                    fmt.Printf("Got a string %q\n", p)
                case int:
                    fmt.Printf("Got an int %d\n", p)
                case struct{}:
                    fmt.Println("Got an empty struct")
                case []string:
                    fmt.Printf("Got a slice of strings %+v\n", p)
                default:
                    fmt.Printf("Type of p is %T. Value %v\n", p, p)
                }
            case <-time.After(time.Second):
                fmt.Println("Empty Loop")
                return
            }
        }
    }()
    ch <- "Hello"
    time.Sleep(time.Second)
    ch <- 123
    time.Sleep(time.Second)
    ch <- struct{}{}
    time.Sleep(time.Second)
    ch <- []string{"Go", "programming"}
    time.Sleep(time.Second)
}</code>
Copy after login

This example demonstrates how you can use type switches to handle different types of values on a channel.

The above is the detailed content of How can I create Go channels that can accept values of multiple different types?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template