Awesome Go

cvt

CategoryUtilities
SubcategoryUtilities
Stars0

Easy and safe convert any value to another type

About cvt

cvt

PkgGoDev Go Report Card Build Status codecov GitHub Mentioned in Awesome Go

Simple, safe conversion of any type, including indirect/custom types.

Documents

https://cvt.shockerli.net

Install

Go >= 1.13

go get -u github.com/shockerli/cvt

Usage

English | 中文

with error

Method __E(): expect handle error, while unable to convert

cvt.IntE("12")          // 12, nil
cvt.Float64E("12.34")   // 12.34, nil
cvt.StringE(12.34)      // "12.34", nil
cvt.BoolE("false")      // false, nil

custom type and pointers

dereferencing pointer and reach the original type

type Name string

var name Name = "jioby"

cvt.StringE(name)       // jioby, nil
cvt.StringE(&name)      // jioby, nil

ignore error

Method __(): ignore error, while convert failed, will return the zero value of type

cvt.Int("12")           // 12(success)
cvt.Int(struct{}{})     // 0(failed)

with default

return the default value, while convert failed

cvt.Int(struct{}{}, 12)     // 12
cvt.Float("hello", 12.34)   // 12.34

more

1000+ unit test cases, for more examples, see *_test.go

License

This project is under the terms of the MIT license.

Thanks

JetBrains Logo (Main) logo

Frequently Asked Questions

What is cvt?

cvt is a Utilities library for the Go programming language. Easy and safe convert any value to another type

How do I install cvt?

Install cvt with the Go module system using `go get shockerli/cvt`. Check the repository for the current installation instructions.

What category does cvt belong to?

cvt is listed under Utilities, specifically Utilities.

← Back to Utilities