您当前的位置: 首页 >  安全

彭世瑜

暂无认证

  • 3浏览

    0关注

    2791博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Golang:cast安全且易用的类型转换工具

彭世瑜 发布时间:2022-09-21 10:10:02 ,浏览量:3

safe and easy casting from one type to another in Go

译文:安全且容易从一种类型转换到另一种类型

文档

  • https://pkg.go.dev/github.com/spf13/cast
  • https://github.com/spf13/cast

安装

go get github.com/spf13/cast

示例

package main

import (
    "fmt"

    "github.com/spf13/cast"
)

func main() {
    // 不处理错误
    i := cast.ToInt("8")
    fmt.Printf("%T: %v", i, i)
    // int: 8

    // 处理错误
    val, err := cast.ToIntE("8")

    if err == nil {
        fmt.Printf("%T: %v", val, val)
        // int: 8
    } else {
        fmt.Println(err)
    }

}

参考 「Go工具箱」一个简单、易用、安全的类型转换工具

关注
打赏
1665367115
查看更多评论
立即登录/注册

微信扫码登录

0.0557s