conv

package
v0.6.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 6, 2021 License: MIT Imports: 1 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertBoolToString

func ConvertBoolToString(b bool) string

ConvertBoolToString converts bool to string. It returns "true", "false"

func ConvertFloat32ToString

func ConvertFloat32ToString(num float32) string

ConvertFloat32ToString convert float32 to string

func ConvertFloat64ToString

func ConvertFloat64ToString(num float64) string

ConvertFloat64ToString convert float32 to string

func ConvertInt16ToString

func ConvertInt16ToString(num int16) string

ConvertInt16ToString converts int16 to string

func ConvertInt32ToString

func ConvertInt32ToString(num int32) string

ConvertInt32ToString converts int32 to string

func ConvertInt64ToString

func ConvertInt64ToString(num int64) string

ConvertInt64ToString converts int64 to string

func ConvertInt8ToString

func ConvertInt8ToString(num int8) string

ConvertInt8ToString converts int8 to string

func ConvertIntToString

func ConvertIntToString(num int) string

ConvertIntToString converts int to string

func ConvertStringToBool

func ConvertStringToBool(boolStr string) (bool, error)

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

func ConvertStringToFloat32(numStr string) (float32, error)

ConvertStringToFloat32 converts string to float32

func ConvertStringToFloat64

func ConvertStringToFloat64(numStr string) (float64, error)

ConvertStringToFloat64 converts string to float64

func ConvertStringToInt

func ConvertStringToInt(numStr string) (int, error)

ConvertStringToInt converts string to int

func ConvertStringToInt16

func ConvertStringToInt16(numStr string) (int16, error)

ConvertStringToInt16 converts string to int16

func ConvertStringToInt32

func ConvertStringToInt32(numStr string) (int32, error)

ConvertStringToInt32 converts string to int32

func ConvertStringToInt64

func ConvertStringToInt64(numStr string) (int64, error)

ConvertStringToInt64 converts string to int64

func ConvertStringToInt8

func ConvertStringToInt8(numStr string) (int8, error)

ConvertStringToInt8 converts string to int8

func ConvertStringToUInt

func ConvertStringToUInt(numStr string) (uint, error)

ConvertStringToUInt converts string to uint

func ConvertStringToUInt16

func ConvertStringToUInt16(numStr string) (uint16, error)

ConvertStringToUInt16 converts string to uint16

func ConvertStringToUInt32

func ConvertStringToUInt32(numStr string) (uint32, error)

ConvertStringToUInt32 converts string to uint32

func ConvertStringToUInt64

func ConvertStringToUInt64(numStr string) (uint64, error)

ConvertStringToUInt64 converts string to uint64

func ConvertStringToUInt8

func ConvertStringToUInt8(numStr string) (uint8, error)

ConvertStringToUInt8 converts string to uint8

func ConvertUInt16ToString

func ConvertUInt16ToString(num uint16) string

ConvertUInt16ToString converts uint to string

func ConvertUInt32ToString

func ConvertUInt32ToString(num uint32) string

ConvertUInt32ToString converts uint to string

func ConvertUInt64ToString

func ConvertUInt64ToString(num uint64) string

ConvertUInt64ToString converts uint to string

func ConvertUInt8ToString

func ConvertUInt8ToString(num uint8) string

ConvertUInt8ToString converts uint to string

func ConvertUIntToString

func ConvertUIntToString(num uint) string

ConvertUIntToString converts uint to string

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL