cvt
GitHubToggle Dark/Light/Auto modeToggle Dark/Light/Auto modeToggle Dark/Light/Auto modeBack to homepage

Getting Started

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

Document statement
__() and __P() are convenient method is based on __E(), so the method of use and the type of support please reference __E () document.

Requirements

  • Go >= 1.13

Install

go get -u github.com/shockerli/cvt

Import

import "github.com/shockerli/cvt"

Usage

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

Pointer

Method __P, return the pointer of converted data

cvt.BoolP("true")   // (*bool)(0x14000126180)(true)

more

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