Documentation
¶
Overview ¶
Package convert provides type conversion utilities for common Go types including int64, float64, string, and struct-to-struct copying.
It also includes helper functions like ToPtr (create pointer to value), ToSafeValue (nil-safe pointer dereferencing), IntToChar (integer to Excel-style column letters), and PascalCaseToCamelCase.
Index ¶
- func CopyStruct(src, dst any, ...) error
- func IntToChar(i int) string
- func PascalCaseToCamelCase(s string) string
- func ToArrInt64(i any) ([]int64, error)
- func ToFloat64(i any) (float64, error)
- func ToInt64(i any) (int64, error)
- func ToPtr[T any](value T) *T
- func ToSafeValue[T any](value any) T
- func ToString(i any) (string, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CopyStruct ¶
func CopyStruct(src, dst any, fieldOpts, typeOpts map[string](func(srcValue reflect.Value) (dstValue reflect.Value, err error))) error
Copy value between 2 struct. Here you can use 2 options:
fieldOpts : you can create your own logic how to copy value between 2 fields with different type (ex. string to time.Time) by field name - as example if field of entity is "CreatedAt" you can use it as key on options typeOpts : you can create your own logic how to copy value between 2 fields with different type (ex. string to time.Time) by type name of destination - as example if package type is "time.Time" you can use "Time" as key on options
func IntToChar ¶
Convert integer to its character representation (eg. 1=A 3=C). Support multiple character for number greater than 26 (eg. 27=AA 703=AAA)
Example ¶
package main
import (
"fmt"
"github.com/alpardfm/go-toolkit/convert"
)
func main() {
fmt.Println(convert.IntToChar(1))
fmt.Println(convert.IntToChar(3))
fmt.Println(convert.IntToChar(27))
}
Output: A C AA
func PascalCaseToCamelCase ¶
Convert Pascal Case string (PascalCase) to Camel Case string (camelCase)
func ToArrInt64 ¶
ToArrInt64 converts a value or slice of values to a slice of int64. If the input is not a slice, it wraps the single converted value in a slice.
func ToFloat64 ¶
ToFloat64 converts any value to a float64, returning an error if the conversion fails.
Example ¶
package main
import (
"fmt"
"github.com/alpardfm/go-toolkit/convert"
)
func main() {
val, err := convert.ToFloat64("3.14")
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Printf("%.2f\n", val)
}
Output: 3.14
func ToInt64 ¶
ToInt64 converts any value to an int64, returning an error if the conversion fails.
Example ¶
package main
import (
"fmt"
"github.com/alpardfm/go-toolkit/convert"
)
func main() {
val, err := convert.ToInt64("42")
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(val)
}
Output: 42
func ToPtr ¶
func ToPtr[T any](value T) *T
ToPtr returns a pointer to the given value. Useful for creating pointers to literals.
Example ¶
package main
import (
"fmt"
"github.com/alpardfm/go-toolkit/convert"
)
func main() {
s := "hello"
ptr := convert.ToPtr(s)
fmt.Println(*ptr)
num := 42
numPtr := convert.ToPtr(num)
fmt.Println(*numPtr)
}
Output: hello 42
func ToSafeValue ¶
Convert any value to `T` data type and if value is not valid or `nil`, will use default value of `T` (null/nil safety) as return value
Example ¶
package main
import (
"fmt"
"github.com/alpardfm/go-toolkit/convert"
)
func main() {
// Returns zero value for nil pointer
var nilPtr *string
result := convert.ToSafeValue[string](nilPtr)
fmt.Printf("%q\n", result)
// Returns actual value for valid input
val := "hello"
result2 := convert.ToSafeValue[string](&val)
fmt.Println(result2)
}
Output: "" hello
func ToString ¶
ToString converts any value to its string representation, returning an error if the conversion fails.
Example ¶
package main
import (
"fmt"
"github.com/alpardfm/go-toolkit/convert"
)
func main() {
val, err := convert.ToString(123)
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(val)
}
Output: 123
Types ¶
This section is empty.