Golang Type Switch: How to match a generic slice/array/map/chan?

likebike

How can I use a Go Type Switch to match a generic slice, array, map, or channel?

package main

import (
    "fmt"
    "reflect"
)

func WhatIsIt(x interface{}) {
    switch X := x.(type) {
        case bool:
            fmt.Printf("Type Switch says %#v is a boolean.\n", X)
        case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
            fmt.Printf("Type Switch says %#v is an integer.\n", X)
        case float32, float64, complex64, complex128:
            fmt.Printf("Type Switch says %#v is a floating-point.\n", X)
        case string:
            fmt.Printf("Type Switch says %#v is a string.\n", X)
        case []interface{}:
            fmt.Printf("TypeSwitch says %#v is a slice.\n", X)
        case map[interface{}]interface{}:
            fmt.Printf("TypeSwitch says %#v is a map.\n", X)
        case chan interface{}:
            fmt.Printf("TypeSwitch says %#v is a channel.\n", X)
        default:
            switch reflect.TypeOf(x).Kind() {
                case reflect.Slice, reflect.Array, reflect.Map, reflect.Chan:
                    fmt.Printf("TypeSwitch was unable to identify this item.  Reflect says %#v is a slice, array, map, or channel.\n", X)
                default:
                    fmt.Printf("Type handler not implemented: %#v\n", X)
            }
    }
}

func main() {
    WhatIsIt(true)
    WhatIsIt(1)
    WhatIsIt(1.5)
    WhatIsIt("abc")
    WhatIsIt([]int{1,2,3})
    WhatIsIt(map[int]int{1:1, 2:2, 3:3})
    WhatIsIt(make(chan int))
}

Here is the output:

Type Switch says true is a boolean.
Type Switch says 1 is an integer.
Type Switch says 1.5 is a floating-point.
Type Switch says "abc" is a string.
TypeSwitch was unable to identify this item.  Reflect says []int{1, 2, 3} is a slice, array, map, or channel.
TypeSwitch was unable to identify this item.  Reflect says map[int]int{1:1, 2:2, 3:3} is a slice, array, map, or channel.
TypeSwitch was unable to identify this item.  Reflect says (chan int)(0x104320c0) is a slice, array, map, or channel.

As you can see from the output, the case []interface{} fails to match the slice that I send in. I need to resort to using the reflect package instead.

If I explicitly write case []int, then it works for my given example, but it's impossible to know all the input types ahead of time, so I need a more general solution. I want to avoid using the reflect package if the Type Switch is able to handle this.

Is there any way to use the Type Switch to determine if an object is a slice/array/map/chan/etc...?

Cerise Limón

Type switches work with specific types. If you cannot enumerate all of the types in the switch, then the only option is to use the reflect package.

v := reflect.ValueOf(x)
switch v.Kind() {
case reflect.Bool:
    fmt.Printf("bool: %v\n", v.Bool())
case reflect.Int, reflect.Int8, reflect.Int32, reflect.Int64:
    fmt.Printf("int: %v\n", v.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint32, reflect.Uint64:
    fmt.Printf("int: %v\n", v.Uint())
case reflect.Float32, reflect.Float64:
    fmt.Printf("float: %v\n", v.Float())
case reflect.String:
    fmt.Printf("string: %v\n", v.String())
case reflect.Slice:
    fmt.Printf("slice: len=%d, %v\n", v.Len(), v.Interface())
case reflect.Map:
    fmt.Printf("map: %v\n", v.Interface())
case reflect.Chan:
    fmt.Printf("chan %v\n", v.Interface())
default:
    fmt.Println(x)
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to pattern match a generic Type

From Dev

How to match scala generic type?

From Dev

How to match scala generic type with trait

From Dev

Returning and using a generic type with match

From Dev

Akka Pattern Match on Generic Type

From Dev

How do I get a generic type to match if Class<Type> uses ? extends Type?

From Dev

How do I get a generic type to match if Class<Type> uses ? extends Type?

From Dev

Switch case statement - Generic associated type

From Dev

how match mixed type of scala higher-kinder types feature with java generic type?

From Dev

switch over enum / iota based type in golang

From Dev

Identical match (with type checking) in switch statement PHP

From Dev

Generic method type and method parameter does not match

From Dev

How to instantiate by generic type?

From Dev

How to create generic type?

From Dev

Java generic method to get value of type by using switch on type

From Java

How do I pattern match on a generic with an abstract inner type in C#?

From Dev

What is the actual type of the variable "t" in a GoLang Type Switch?

From Dev

How to switch between low latency and generic kernels?

From Dev

How to make a generic method that take a generic type

From Dev

How to return class type of a generic class with generic?

From Dev

How to check generic type in generic method

From Dev

Perform pattern match on a generic value with type matching the return type

From Dev

How to match such type

From Dev

How to switch on reflect.Type?

From Dev

How to get generic type from generic definition and generic arguments?

From Dev

How to get generic type from generic definition and generic arguments?

From Dev

How to post generic type in jersey?

From Dev

How to implement a constructor for a generic type

From Dev

How to define generic type in Scala?

Related Related

  1. 1

    How to pattern match a generic Type

  2. 2

    How to match scala generic type?

  3. 3

    How to match scala generic type with trait

  4. 4

    Returning and using a generic type with match

  5. 5

    Akka Pattern Match on Generic Type

  6. 6

    How do I get a generic type to match if Class<Type> uses ? extends Type?

  7. 7

    How do I get a generic type to match if Class<Type> uses ? extends Type?

  8. 8

    Switch case statement - Generic associated type

  9. 9

    how match mixed type of scala higher-kinder types feature with java generic type?

  10. 10

    switch over enum / iota based type in golang

  11. 11

    Identical match (with type checking) in switch statement PHP

  12. 12

    Generic method type and method parameter does not match

  13. 13

    How to instantiate by generic type?

  14. 14

    How to create generic type?

  15. 15

    Java generic method to get value of type by using switch on type

  16. 16

    How do I pattern match on a generic with an abstract inner type in C#?

  17. 17

    What is the actual type of the variable "t" in a GoLang Type Switch?

  18. 18

    How to switch between low latency and generic kernels?

  19. 19

    How to make a generic method that take a generic type

  20. 20

    How to return class type of a generic class with generic?

  21. 21

    How to check generic type in generic method

  22. 22

    Perform pattern match on a generic value with type matching the return type

  23. 23

    How to match such type

  24. 24

    How to switch on reflect.Type?

  25. 25

    How to get generic type from generic definition and generic arguments?

  26. 26

    How to get generic type from generic definition and generic arguments?

  27. 27

    How to post generic type in jersey?

  28. 28

    How to implement a constructor for a generic type

  29. 29

    How to define generic type in Scala?

HotTag

Archive