Golang

关注公众号 jb51net

关闭
首页 > 脚本专栏 > Golang > Go type switch用法

Go type switch的四种用法小结

作者:运维开发笔记

本文详细讲解type switch的四种用法:基本语法、变量赋值、匹配接口类型和多类型合并,通过实例演示如何优雅地处理未知类型数据,避免手动断言,让你的代码更简洁安全

1. type switch 基本语法 — switch value.(type)

type switch 根据 interface{} 值的实际类型进行分支判断,类似"类型版"的 switch:

package main

import "fmt"

func describeType(value interface{}) {
    switch value.(type) {
    case string:
        v := value.(string)
        fmt.Println("It's a string with length:", len(v))
    case int:
        v := value.(int)
        fmt.Println("It's an integer with value:", v)
    case bool:
        v := value.(bool)
        fmt.Println("It's a boolean with value:", v)
    case []int:
        v := value.([]int)
        fmt.Println("It's an int slice with length:", len(v))
    default:
        fmt.Println("Unknown type")
    }
}

func main() {
    fmt.Println("=== describeType tests ===")
    describeType("hello world")
    describeType(42)
    describeType(true)
    describeType([]int{1, 2, 3, 4, 5})
    describeType(3.14)
}

执行结果:

=== describeType tests ===
It's a string with length: 11
It's an integer with value: 42
It's a boolean with value: true
It's an int slice with length: 5
Unknown type

要点:

2. type switch 变量赋值 — switch v := value.(type)

推荐写法:在 switch 中直接声明变量 v,v 在每个 case 分支中自动具有对应类型,无需手动断言:

package main

import (
    "fmt"
    "reflect"
)

func processValue(value interface{}) {
    switch v := value.(type) {
    case string:
        // v 自动是 string 类型,可以直接使用 len(v) 等字符串操作
        if len(v) > 5 {
            fmt.Println("Long string:", v)
        } else {
            fmt.Println("Short string:", v)
        }
    case int:
        // v 自动是 int 类型,可以直接做数值运算
        if v > 0 {
            fmt.Println("Positive:", v)
        } else {
            fmt.Println("Non-positive:", v)
        }
    case float64:
        // v 自动是 float64 类型
        fmt.Printf("Float with 2 decimal places: %.2f\n", v)
    default:
        // v 是 interface{} 类型,需要用 reflect 获取类型信息
        fmt.Println("Cannot process type:", reflect.TypeOf(value))
    }
}

func main() {
    fmt.Println("=== processValue tests ===")
    processValue("short")
    processValue("this is a long string")
    processValue(-10)
    processValue(25)
    processValue(3.14159)
    processValue([]string{"not", "handled"})
}

执行结果:

=== processValue tests ===
Short string: short
Long string: this is a long string
Non-positive: -10
Positive: 25
Float with 2 decimal places: 3.14
Cannot process type: []string

要点:

3. type switch 处理接口类型 — 匹配 error 接口

type switch 的 case 不仅可以是具体类型(string、int),还可以是接口类型(error、io.Reader 等):

package main

import "fmt"

func handleError(value interface{}) {
    switch v := value.(type) {
    case string:
        fmt.Println("String error:", v)
    case error:
        fmt.Println("Error type:", v.Error())
    case nil:
        fmt.Println("No error")
    default:
        fmt.Println("Unknown error type")
    }
}

func main() {
    fmt.Println("=== handleError tests ===")
    handleError("file not found")
    handleError(fmt.Errorf("custom error"))
    handleError(nil)
    handleError(123)
}

执行结果:

=== handleError tests ===
String error: file not found
Error type: custom error
No error
Unknown error type

要点:

4. type switch 多类型合并 — 一个 case 匹配多种类型

多个类型可以写在同一个 case 中,用逗号分隔:

package main

import "fmt"

func main() {
    fmt.Println("=== Multiple types in one case ===")
    handleInteger := func(value interface{}) {
        switch v := value.(type) {
        case int, int32, int64:
            fmt.Printf("Integer type value: %v (type: %T)\n", v, v)
        default:
            fmt.Println("Not an integer type")
        }
    }

    handleInteger(10)
    handleInteger(int32(20))
    handleInteger(int64(30))
    handleInteger("not an integer")
}

执行结果:

=== Multiple types in one case ===
Integer type value: 10 (type: int)
Integer type value: 20 (type: int32)
Integer type value: 30 (type: int64)
Not an integer type

要点:

知识点总结

知识点关键概念
type switch 基本语法switch value.(type)
变量赋值写法switch v := value.(type)
匹配接口类型case error:
多类型合并case int, int32, int64:
.(type) 限制.(type)

到此这篇关于Go type switch的四种用法小结的文章就介绍到这了,更多相关Go type switch用法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文