convert

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 26, 2026 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

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

func IntToChar(i int) string

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

func PascalCaseToCamelCase(s string) string

Convert Pascal Case string (PascalCase) to Camel Case string (camelCase)

func ToArrInt64

func ToArrInt64(i any) ([]int64, error)

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

func ToFloat64(i any) (float64, error)

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

func ToInt64(i any) (int64, error)

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

func ToSafeValue[T any](value any) T

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

func ToString(i any) (string, error)

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.

Jump to

Keyboard shortcuts

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