convertutils

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2025 License: Unlicense Imports: 4 Imported by: 0

Documentation

Overview

Package convertutils 提供类型转换相关的工具函数 Package convertutils provides type conversion utility functions

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BoolToString

func BoolToString(b bool) string

BoolToString 将bool转换为字符串

参数 / Parameters:

  • b: 布尔值 / boolean

返回值 / Returns:

  • string: 转换后的字符串 / converted string

示例 / Example:

BoolToString(true) // "true"

BoolToString converts a bool to string

func DeepCopy

func DeepCopy(src, dst interface{}) error

DeepCopy 深拷贝任意对象(通过JSON序列化/反序列化)

参数 / Parameters:

  • src: 源对象(必须是指针) / source object (must be a pointer)
  • dst: 目标对象(必须是指针) / destination object (must be a pointer)

返回值 / Returns:

  • error: 如果拷贝失败则返回错误 / error if copy fails

示例 / Example:

type User struct { Name string; Age int }
src := &User{Name: "John", Age: 30}
var dst User
DeepCopy(src, &dst)

DeepCopy performs deep copy of any object via JSON serialization

func Float64ToString

func Float64ToString(f float64, prec int) string

Float64ToString 将float64转换为字符串

参数 / Parameters:

  • f: 浮点数 / float
  • prec: 精度(小数位数) / precision (decimal places)

返回值 / Returns:

  • string: 转换后的字符串 / converted string

示例 / Example:

Float64ToString(123.456, 2) // "123.46"

Float64ToString converts a float64 to string

func Int64ToString

func Int64ToString(i int64, base int) string

Int64ToString 将int64转换为字符串

参数 / Parameters:

  • i: int64整数 / int64 integer
  • base: 进制(10表示十进制) / base (10 for decimal)

返回值 / Returns:

  • string: 转换后的字符串 / converted string

示例 / Example:

Int64ToString(123, 10) // "123"

Int64ToString converts an int64 to string

func IntToString

func IntToString(i int) string

IntToString 将整数转换为字符串

参数 / Parameters:

  • i: 整数 / integer

返回值 / Returns:

  • string: 转换后的字符串 / converted string

示例 / Example:

IntToString(123) // "123"

IntToString converts an int to string

func StringToBool

func StringToBool(s string) (bool, error)

StringToBool 将字符串转换为bool

参数 / Parameters:

  • s: 字符串 / string

返回值 / Returns:

  • bool: 转换后的布尔值 / converted boolean
  • error: 如果转换失败则返回错误 / error if conversion fails

示例 / Example:

StringToBool("true") // true, nil

StringToBool converts a string to bool

func StringToFloat64

func StringToFloat64(s string) (float64, error)

StringToFloat64 将字符串转换为float64

参数 / Parameters:

  • s: 字符串 / string

返回值 / Returns:

  • float64: 转换后的浮点数 / converted float
  • error: 如果转换失败则返回错误 / error if conversion fails

示例 / Example:

StringToFloat64("123.45") // 123.45, nil

StringToFloat64 converts a string to float64

func StringToInt

func StringToInt(s string) (int, error)

StringToInt 将字符串转换为整数

参数 / Parameters:

  • s: 字符串 / string

返回值 / Returns:

  • int: 转换后的整数 / converted integer
  • error: 如果转换失败则返回错误 / error if conversion fails

示例 / Example:

StringToInt("123") // 123, nil

StringToInt converts a string to int

func StringToInt64

func StringToInt64(s string, base int) (int64, error)

StringToInt64 将字符串转换为int64

参数 / Parameters:

  • s: 字符串 / string
  • base: 进制(10表示十进制) / base (10 for decimal)

返回值 / Returns:

  • int64: 转换后的整数 / converted integer
  • error: 如果转换失败则返回错误 / error if conversion fails

示例 / Example:

StringToInt64("123", 10) // 123, nil

StringToInt64 converts a string to int64

func ToInt

func ToInt(v interface{}) (int, error)

ToInt 将任意类型转换为int(支持string, int, int64, float64)

参数 / Parameters:

  • v: 要转换的值 / value to convert

返回值 / Returns:

  • int: 转换后的整数 / converted integer
  • error: 如果转换失败则返回错误 / error if conversion fails

示例 / Example:

ToInt("123") // 123, nil
ToInt(123)   // 123, nil

ToInt converts any type to int (supports string, int, int64, float64)

func ToString

func ToString(v interface{}) string

ToString 将任意类型转换为string

参数 / Parameters:

  • v: 要转换的值 / value to convert

返回值 / Returns:

  • string: 转换后的字符串 / converted string

示例 / Example:

ToString(123)    // "123"
ToString(true)   // "true"

ToString converts any type 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