Documentation ¶
Overview ¶
Package conv provides a string and number converter. It includes the following data types: int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, bool.
Example Usage ¶
The following is a complete example using logger package
import ( conv "github.com/phamtai97/go-utils/utils/convertor" "github.com/phamtai97/go-utils/utils/logger" "go.uber.org/zap" ) func main() { logger.InitProduction("") numInt, err := conv.ConvertStringToInt("123456") if err != nil { logger.Fatal("Failed to convert string to int") } logger.Info("Convert string to int", zap.Int("Value", numInt)) numInt32, err := conv.ConvertStringToInt32("123456") if err != nil { logger.Fatal("Failed to convert string to int32") } logger.Info("Convert string to int32", zap.Int32("Value", numInt32)) numInt64, err := conv.ConvertStringToInt64("123456") if err != nil { logger.Fatal("Failed to convert string to int64") } logger.Info("Convert string to int64", zap.Int64("Value", numInt64)) bTrue, err := conv.ConvertStringToBool("TRUE") if err != nil { logger.Fatal("Failed to convert string to bool") } logger.Info("Convert string to bool", zap.Bool("Value", bTrue)) bFalse, err := conv.ConvertStringToBool("false") if err != nil { logger.Fatal("Failed to convert string to bool") } logger.Info("Convert string to bool", zap.Bool("Value", bFalse)) numFloat32, err := conv.ConvertStringToFloat32("1.23243") if err != nil { logger.Fatal("Failed to convert string to float32") } logger.Info("Convert string to float32", zap.Float32("Value", numFloat32)) numFloat64, err := conv.ConvertStringToFloat64("1.23243") if err != nil { logger.Fatal("Failed to convert string to float64") } logger.Info("Convert string to float64", zap.Float64("Value", numFloat64)) numUInt, err := conv.ConvertStringToUInt("123456") if err != nil { logger.Fatal("Failed to convert string to uint") } logger.Info("Convert string to uint", zap.Uint("Value", numUInt)) strInt := conv.ConvertIntToString(123456) logger.Info("Convert int to string", zap.String("Value", strInt)) strInt32 := conv.ConvertInt32ToString(123456) logger.Info("Convert int32 to string", zap.String("Value", strInt32)) strInt64 := conv.ConvertInt64ToString(123456) logger.Info("Convert int64 to string", zap.String("Value", strInt64)) strBool := conv.ConvertBoolToString(true) logger.Info("Convert bool to string", zap.String("Value", strBool)) strFloat32 := conv.ConvertFloat32ToString(1.2345) logger.Info("Convert float32 to string", zap.String("Value", strFloat32)) strFloat64 := conv.ConvertFloat64ToString(1.2345) logger.Info("Convert float64 to string", zap.String("Value", strFloat64)) strUInt := conv.ConvertUIntToString(123456) logger.Info("Convert uint to string", zap.String("Value", strUInt)) }
Index ¶
- func ConvertBoolToString(b bool) string
- func ConvertFloat32ToString(num float32) string
- func ConvertFloat64ToString(num float64) string
- func ConvertInt16ToString(num int16) string
- func ConvertInt32ToString(num int32) string
- func ConvertInt64ToString(num int64) string
- func ConvertInt8ToString(num int8) string
- func ConvertIntToString(num int) string
- func ConvertStringToBool(boolStr string) (bool, error)
- func ConvertStringToFloat32(numStr string) (float32, error)
- func ConvertStringToFloat64(numStr string) (float64, error)
- func ConvertStringToInt(numStr string) (int, error)
- func ConvertStringToInt16(numStr string) (int16, error)
- func ConvertStringToInt32(numStr string) (int32, error)
- func ConvertStringToInt64(numStr string) (int64, error)
- func ConvertStringToInt8(numStr string) (int8, error)
- func ConvertStringToUInt(numStr string) (uint, error)
- func ConvertStringToUInt16(numStr string) (uint16, error)
- func ConvertStringToUInt32(numStr string) (uint32, error)
- func ConvertStringToUInt64(numStr string) (uint64, error)
- func ConvertStringToUInt8(numStr string) (uint8, error)
- func ConvertUInt16ToString(num uint16) string
- func ConvertUInt32ToString(num uint32) string
- func ConvertUInt64ToString(num uint64) string
- func ConvertUInt8ToString(num uint8) string
- func ConvertUIntToString(num uint) string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConvertBoolToString ¶
ConvertBoolToString converts bool to string. It returns "true", "false"
func ConvertFloat32ToString ¶
ConvertFloat32ToString convert float32 to string
func ConvertFloat64ToString ¶
ConvertFloat64ToString convert float32 to string
func ConvertInt16ToString ¶
ConvertInt16ToString converts int16 to string
func ConvertInt32ToString ¶
ConvertInt32ToString converts int32 to string
func ConvertInt64ToString ¶
ConvertInt64ToString converts int64 to string
func ConvertInt8ToString ¶
ConvertInt8ToString converts int8 to string
func ConvertIntToString ¶
ConvertIntToString converts int to string
func ConvertStringToBool ¶
ConvertStringToBool converts string to bool. It accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False. Any other value returns an error.
func ConvertStringToFloat32 ¶
ConvertStringToFloat32 converts string to float32
func ConvertStringToFloat64 ¶
ConvertStringToFloat64 converts string to float64
func ConvertStringToInt ¶
ConvertStringToInt converts string to int
func ConvertStringToInt16 ¶
ConvertStringToInt16 converts string to int16
func ConvertStringToInt32 ¶
ConvertStringToInt32 converts string to int32
func ConvertStringToInt64 ¶
ConvertStringToInt64 converts string to int64
func ConvertStringToInt8 ¶
ConvertStringToInt8 converts string to int8
func ConvertStringToUInt ¶
ConvertStringToUInt converts string to uint
func ConvertStringToUInt16 ¶
ConvertStringToUInt16 converts string to uint16
func ConvertStringToUInt32 ¶
ConvertStringToUInt32 converts string to uint32
func ConvertStringToUInt64 ¶
ConvertStringToUInt64 converts string to uint64
func ConvertStringToUInt8 ¶
ConvertStringToUInt8 converts string to uint8
func ConvertUInt16ToString ¶
ConvertUInt16ToString converts uint to string
func ConvertUInt32ToString ¶
ConvertUInt32ToString converts uint to string
func ConvertUInt64ToString ¶
ConvertUInt64ToString converts uint to string
func ConvertUInt8ToString ¶
ConvertUInt8ToString converts uint to string
func ConvertUIntToString ¶
ConvertUIntToString converts uint to string
Types ¶
This section is empty.