# cvt **Repository Path**: shockerli/cvt ## Basic Information - **Project Name**: cvt - **Description**: 一个简单、安全、高效的转换任意数据类型的 Go 语言工具包,支持自定义类型、提取结构体字段和值 - **Primary Language**: Go - **License**: MIT - **Default Branch**: master - **Homepage**: https://cvt.shockerli.net - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-03-26 - **Last Updated**: 2024-10-22 ## Categories & Tags **Categories**: Uncategorized **Tags**: Go语言, Library ## README # cvt [![PkgGoDev](https://pkg.go.dev/badge/github.com/shockerli/cvt)](https://pkg.go.dev/github.com/shockerli/cvt) [![Go Report Card](https://goreportcard.com/badge/github.com/shockerli/cvt)](https://goreportcard.com/report/github.com/shockerli/cvt) [![Build Status](https://travis-ci.com/shockerli/cvt.svg?branch=master)](https://travis-ci.com/shockerli/cvt) [![codecov](https://codecov.io/gh/shockerli/cvt/branch/master/graph/badge.svg)](https://codecov.io/gh/shockerli/cvt) ![GitHub](https://img.shields.io/github/license/shockerli/cvt) [![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go) > 一个简单、安全、高效的转换任意数据类型的 Go 语言工具包,支持自定义类型、提取结构体字段和值 ## 帮助文档 https://cvt.shockerli.net ## 安装 > Go >= 1.13 ```go go get -u github.com/shockerli/cvt ``` ## 使用 中文 | [English](README.md) ### 支持 `error` > 以 `E` 结尾的方法 `__E()`: 当转换失败时会返回错误 ```go cvt.IntE("12") // 12, nil cvt.Float64E("12.34") // 12.34, nil cvt.StringE(12.34) // "12.34", nil cvt.BoolE("false") // false, nil ``` ### 自定义类型、指针类型 > 自动解引用,并找到基本类型,完全支持自定义类型的转换 ```go type Name string var name Name = "jioby" cvt.StringE(name) // jioby, nil cvt.StringE(&name) // jioby, nil ``` ### 忽略 `error` > 名称不以 `E` 结尾的方法,如果转换失败,不会返回错误,会返回零值 ```go cvt.Int("12") // 12(success) cvt.Int(struct{}{}) // 0(failed) ``` ### 默认值 > 如果转换失败,返回默认值 ```go cvt.Int(struct{}{}, 12) // 12 cvt.Float("hello", 12.34) // 12.34 ``` ### 更多示例 > 上千个单元测试用例,覆盖率近100%,所有示例可通过单元测试了解:`*_test.go` ## 开源协议 本项目基于 [MIT](LICENSE) 协议开放源代码。 ## 感谢 - [JetBrains Open Source Support](https://jb.gg/OpenSourceSupport) ![JetBrains Logo (Main) logo](https://resources.jetbrains.com/storage/products/company/brand/logos/jb_beam.svg)