cvt

cvt是一个简单、安全、高效的Go语言工具包,用于任意数据类型的安全转换。支持自定义类型、指针类型、默认值设置,并提供丰富的API覆盖基本数据类型转换。具有99%的测试覆盖率,确保稳定可靠。

Check out cvt on CurateClick

• Copy the embed code to showcase this product on your website

• Share on X to spread the word about this amazing tool

cvt一个简单、安全、高效的转换任意数据类型的 Go 语言工具包,支持自定义类型、提取结构体字段和值GitHub: https://github.com/shockerli/cvt## 安装gogo get -u github.com/shockerli/cvt## 使用### 支持 error> 以 E 结尾的方法 __E(): 当转换失败时会返回错误gocvt.IntE("12") // 12, nilcvt.Float64E("12.34") // 12.34, nilcvt.StringE(12.34) // "12.34", nilcvt.BoolE("false") // false, nil### 自定义类型、指针类型> 自动解引用,并找到基本类型,完全支持自定义类型的转换gotype Name stringvar name Name = "jioby"cvt.StringE(name) // jioby, nilcvt.StringE(&name) // jioby, nil### 忽略 error> 名称不以 E 结尾的方法,如果转换失败,不会返回错误,会返回零值gocvt.Int("12") // 12(success)cvt.Int(struct{}{}) // 0(failed)### 默认值> 如果转换失败,返回默认值gocvt.Int(struct{}{}, 12) // 12cvt.Float("hello", 12.34) // 12.34### 更多示例> 超过1000个单元测试用例,覆盖率99%,所有示例可通过单元测试了解:*_test.go## API### bool- Bool / BoolE### int- Int / IntE- Int8 / Int8E- Int16 / Int16E- Int32 / Int32E- Int64 / Int64E- Uint / UintE- Uint8 / Uint8E- Uint16 / Uint16E- Uint32 / Uint32E- Uint64 / Uint64E### string- String / StringE### float- Float32 / Float32E- Float64 / Float64E### time- Time / TimeE### slice- ColumnsE: 类似于 PHP 中的 array_columnFieldE 函数的切片版本,返回 []interface{}- FieldE: 取 mapstruct 的字段值,返回 interface{}- KeysE: 取 map 的键名,返回 []interface{}- Slice / SliceE: 转换成 []interface{}- SliceIntE: 转换成 []int- SliceInt64E: 转换成 []int64- SliceFloat64E: 转换成 []float64- SliceStringE: 转换成 []string

Latest Weekly Picks

make.ink favicon
Weekly Pick

Your AI tattoo generator for pro-grade concepts

Leo Wade
sellfy.com favicon
Weekly Pick

A code-free online store builder to turn views into revenue. Sell digital products, subscriptions, and merch, without fees or hassle.

Maris
www.videotoblog.ai favicon
Weekly Pick

Convert videos into awesome blog posts.

Video To Blog
www.sellerpic.ai favicon
Weekly Pick

SellerPic is the all-in-one AI design platform for e-commerce

SellerPic
fastimage.ai favicon
Weekly Pick

Fast Image AI instantly transforms your photos into stunning styles like Ghibli, Sketch, and Pixar. Effortlessly control image elements and create amazing effects with just one click.

Fast AI Team
linkedinpro.daisy.so favicon
Weekly Pick

AI-powered tool that transforms casual photos into professional LinkedIn headshots instantly. No photographer needed—just upload and download.

Gabriel
crevas.ai favicon
Weekly Pick

Crevas unifies Veo 3, Sora 2, Nano Banana and more into one intuitive canvas — so filmmakers can script, prompt, and generate cinematic stories without switching tools or losing consistency.

Spark Alpha
fotominiatur.com favicon
Weekly Pick

AI Foto Edit - Text to Image & Image Edit

foto miniatur