• Go 将interface转为int, string, slice, struct等类型


    在golang中,interface{}允许接纳任意值,intstringstruct,slice等,因此我可以很简单的将值传递到interface{}

    但是当我们将任意类型传入到test函数中转为interface后,经常需要进行一系列操作interface不具备的方法(即传入的User结构体,interface本身也没有所谓的Name属性),此时就需要用到interface特性type assertionstype switches,来将其转换为回原本传入的类型

    package main
    import (
        "fmt"
    )
    type User struct{
        Name string
    }
    func main() {
        any := User{
            Name: "fidding",
        }
        test(any)
        any2 := "fidding"
        test(any2)
        any3 := int32(123)
        test(any3)
        any4 := int64(123)
        test(any4)
        any5 := []int{1, 2, 3, 4, 5}
        test(any5)
    }
    func test(value interface{}) {
        switch value.(type) {
        case string:
            // 将interface转为string字符串类型
            op, ok := value.(string)
            fmt.Println(op, ok)
        case int32:
            // 将interface转为int32类型
            op, ok := value.(int32)
            fmt.Println(op, ok)
        case int64:
            // 将interface转为int64类型
            op, ok := value.(int64)
            fmt.Println(op, ok)
        case User:
            // 将interface转为User struct类型,并使用其Name对象
            op, ok := value.(User)
            fmt.Println(op.Name, ok)
        case []int:
            // 将interface转为切片类型
            op := make([]int, 0)  //[]
            op = value.([]int)
            fmt.Println(op)
        default:
            fmt.Println("unknown")
        }
    }

    输出结果:

    可以看到我们可以对interface使用.()并在括号中传入想要解析的任何类型,例如

    // 如果转换失败ok=false,转换成功ok=true
    res, ok := anyInterface.(someType)

    不确定interface类型时候,使用anyInterface.(type)结合switch case来做判断。

    现在再回过头看上面的例子,是不是更清楚了呢

  • 相关阅读:
    向Url发送post请求传递参数
    表格列转行
    C#匹配HTML中的图片地址
    C# 正则表达式(备忘录)
    将天文数字转换成中文大写
    搜索引擎优化排名因素小结
    WPF概述
    将中文汉字转换成拼音(全拼)
    提取网页中的链接并生成xml
    快速排序算法的实现
  • 原文地址:https://www.cnblogs.com/zhaoyingjie/p/15773973.html
Copyright © 2020-2023  润新知