convert

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2025 License: MIT Imports: 11 Imported by: 7

README

convert

Comprehensive type conversion utilities for Go, including basic types, pointers, collections, and specialized conversions.

Features

  • Convert between basic types (string, bool, int, float)
  • Pointer conversion utilities with generics
  • Collection conversion (slice/map to pointer variants and vice versa)
  • String manipulation utilities (snake_case, TitleCase, MD5 hashing)
  • Binary conversion utilities (float64 to bytes)
  • Big5 to UTF-8 encoding conversion

Usage

Basic Type Conversions
package main

import (
    "fmt"
    "github.com/appleboy/com/convert"
)

func main() {
    // Convert any value to string
    str := convert.ToString(123)
    fmt.Println(str) // Output: 123
    
    str = convert.ToString(true)
    fmt.Println(str) // Output: true
    
    // Convert to boolean
    b := convert.ToBool("true")
    fmt.Println(b) // Output: true
    
    b = convert.ToBool(1)
    fmt.Println(b) // Output: true
    
    b = convert.ToBool("")
    fmt.Println(b) // Output: false
    
    // Convert to int (returns interface{} - nil if conversion fails)
    if i := convert.ToInt("123"); i != nil {
        fmt.Println(i) // Output: 123
    }
    
    // Convert to float
    if f := convert.ToFloat("123.45"); f != nil {
        fmt.Println(f) // Output: 123.45
    }
}
Pointer Utilities
package main

import (
    "fmt"
    "github.com/appleboy/com/convert"
)

func main() {
    // Convert value to pointer
    value := 42
    ptr := convert.ToPtr(value)
    fmt.Println(*ptr) // Output: 42
    
    // Convert pointer to value (returns zero value if nil)
    result := convert.FromPtr(ptr)
    fmt.Println(result) // Output: 42
    
    // Handle nil pointer
    var nilPtr *int
    result = convert.FromPtr(nilPtr)
    fmt.Println(result) // Output: 0
}
Collection Conversions
package main

import (
    "fmt"
    "github.com/appleboy/com/convert"
)

func main() {
    // Convert slice to pointer slice
    values := []int{1, 2, 3}
    ptrs := convert.SliceToPtrSlice(values)
    fmt.Println(*ptrs[0]) // Output: 1
    
    // Convert pointer slice back to values
    backToValues := convert.PtrSliceToSlice(ptrs)
    fmt.Println(backToValues) // Output: [1 2 3]
    
    // Convert map to pointer map
    valueMap := map[string]int{"a": 1, "b": 2}
    ptrMap := convert.MapToPtrMap(valueMap)
    fmt.Println(*ptrMap["a"]) // Output: 1
    
    // Convert pointer map back to values (skips nil pointers)
    backToMap := convert.PtrMapToMap(ptrMap)
    fmt.Println(backToMap) // Output: map[a:1 b:2]
}
String Utilities
package main

import (
    "fmt"
    "github.com/appleboy/com/convert"
)

func main() {
    // Convert to snake_case
    snake := convert.SnakeCasedName("FooBarTest")
    fmt.Println(snake) // Output: foo_bar_test
    
    // Convert to TitleCase
    title := convert.TitleCasedName("foo_bar_test")
    fmt.Println(title) // Output: FooBarTest
    
    // MD5 hash
    hash := convert.MD5Hash("hello world")
    fmt.Println(hash) // Output: 5d41402abc4b2a76b9719d911017c592
}
Binary Conversion
package main

import (
    "fmt"
    "github.com/appleboy/com/convert"
)

func main() {
    // Convert float64 to bytes (BigEndian)
    f := 123.456
    bytes := convert.Float64ToByte(f)
    fmt.Printf("Bytes: %v\n", bytes)
    
    // Convert bytes back to float64
    back := convert.ByteToFloat64(bytes)
    fmt.Printf("Float: %f\n", back) // Output: Float: 123.456000
}
Encoding Conversion
package main

import (
    "fmt"
    "github.com/appleboy/com/convert"
)

func main() {
    // Convert Big5 encoded string to UTF-8
    big5Str := "\xa7A\xa6n" // "你好" in Big5
    utf8Str := convert.ConvertBig5ToUTF8(big5Str)
    fmt.Println(utf8Str) // Output: 你好
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ByteToFloat64 added in v0.1.0

func ByteToFloat64(bytes []byte) float64

ByteToFloat64 converts an 8-byte slice in BigEndian order back to a float64 value. - Panics if the input length is not 8. - Useful for binary deserialization and network data parsing.

func ConvertBig5ToUTF8 added in v0.2.1

func ConvertBig5ToUTF8(s string) string

ConvertBig5ToUTF8 converts a string encoded in Big5 to a UTF-8 encoded string. The input parameter s must be a string encoded in Big5, and the function returns the corresponding UTF-8 string. This function uses the Go standard library's transform package for conversion, which results in a string allocation. If the conversion fails (e.g., if the input is not valid Big5), the original string is returned and no panic occurs.

Usage Example:

big5Str := "\xa4\xa4\xa4\xe5" // "中文" in Big5 encoding
utf8Str := ConvertBig5ToUTF8(big5Str)
fmt.Println(utf8Str) // Output: 中文

func Float64ToByte added in v0.1.0

func Float64ToByte(f float64) []byte

Float64ToByte converts a float64 value to an 8-byte slice in BigEndian order. - Useful for binary serialization and network transmission. - Reference: https://stackoverflow.com/questions/43693360/convert-float64-to-byte-array

func FromPtr added in v0.3.0

func FromPtr[T any](ptr *T) T

FromPtr takes a pointer to a value and returns the value itself. If the pointer is nil, it returns the zero value of the type.

T: The type of the value being dereferenced. ptr: The pointer to the value.

Returns the value pointed to by the pointer, or the zero value if the pointer is nil.

func MD5Hash added in v0.0.3

func MD5Hash(text string) string

MD5Hash computes the MD5 hash of the input string and returns a 32-character hexadecimal string. - Useful for data validation, generating unique identifiers, etc. - Warning: MD5 is not recommended for password hashing or security-sensitive use cases.

func MapToPtrMap added in v1.0.0

func MapToPtrMap[T any](src map[string]T) map[string]*T

MapToPtrMap converts a map of values (map[string]T) to a map of pointers (map[string]*T). Each value in the result points to a copy of the corresponding value in the input map.

func PtrMapToMap added in v1.0.0

func PtrMapToMap[T any](src map[string]*T) map[string]T

PtrMapToMap converts a map of pointers (map[string]*T) to a map of values (map[string]T). If a value in the input map is nil, the key is omitted in the result.

func PtrSliceToSlice added in v1.0.0

func PtrSliceToSlice[T any](src []*T) []T

PtrSliceToSlice converts a slice of pointers ([]*T) to a slice of values ([]T). If an element in the input slice is nil, the zero value of T is used.

func SliceToPtrSlice added in v1.0.0

func SliceToPtrSlice[T any](src []T) []*T

SliceToPtrSlice converts a slice of values ([]T) to a slice of pointers ([]*T). Each element in the result points to the corresponding element in the input slice. Note: The pointers are valid only as long as the input slice is not modified.

func SnakeCasedName added in v0.0.9

func SnakeCasedName(name string) string

SnakeCasedName converts a string to snake_case format and supports Unicode characters. - Each uppercase English letter (A-Z) is converted to lowercase and prefixed with an underscore if not at the start. - Only English letters are affected; other Unicode characters (e.g., Chinese) are preserved as-is. - Example: FooBar -> foo_bar, 你好World -> 你好_world

func TitleCasedName added in v0.0.9

func TitleCasedName(name string) string

TitleCasedName converts a snake_case string to TitleCase format and supports Unicode characters. - Each English letter following an underscore is capitalized, and underscores are removed. - Only English letters are affected; other Unicode characters (e.g., Chinese) are preserved as-is. - Example: foo_bar -> FooBar, hello_世界 -> Hello世界

func ToBool

func ToBool(value interface{}) bool

ToBool convert any type to boolean

func ToFloat

func ToFloat(value interface{}) interface{}

ToFloat convert any type to float

func ToInt

func ToInt(value interface{}) interface{}

ToInt convert any type to int

func ToPtr added in v0.2.0

func ToPtr[T any](value T) *T

ToPtr takes a value of any type and returns a pointer to that value. This is useful for converting a value to a pointer in a generic way.

T: The type of the value being converted. value: The value to be converted to a pointer.

Returns a pointer to the provided value.

func ToString

func ToString(value interface{}) string

ToString convert 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