convertor

package
v0.1.13 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2024 License: MIT, MIT Imports: 11 Imported by: 0

Documentation

Overview

Package convertor implements some functions to convert data.

Package convertor implements some functions to convert data.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ColorHexToRGB

func ColorHexToRGB(colorHex string) (red, green, blue int)

ColorHexToRGB convert hex color to rgb color. Play: https://go.dev/play/p/o7_ft-JCJBV

Example
colorHex := "#003366"
r, g, b := ColorHexToRGB(colorHex)

fmt.Println(r, g, b)
Output:

0 51 102

func ColorRGBToHex

func ColorRGBToHex(red, green, blue int) string

ColorRGBToHex convert rgb color to hex color. Play: https://go.dev/play/p/nzKS2Ro87J1

Example
r := 0
g := 51
b := 102
colorHex := ColorRGBToHex(r, g, b)

fmt.Println(colorHex)
Output:

#003366

func CopyProperties

func CopyProperties[T, U any](dst T, src U) (err error)

CopyProperties copies each field from the source into the destination. It recursively copies struct pointers and interfaces that contain struct pointers. Play: https://go.dev/play/p/FOVY3XJL-6B

Example
type Address struct {
	Country string
	ZipCode string
}

type User struct {
	Name   string
	Age    int
	Role   string
	Addr   Address
	Hobbys []string
	salary int
}

type Employee struct {
	Name   string
	Age    int
	Role   string
	Addr   Address
	Hobbys []string
	salary int
}

user := User{Name: "user001", Age: 10, Role: "Admin", Addr: Address{Country: "CN", ZipCode: "001"}, Hobbys: []string{"a", "b"}, salary: 1000}

employee1 := Employee{}
err := CopyProperties(&employee1, &user)
if err != nil {
	return
}

employee2 := Employee{Name: "employee001", Age: 20, Role: "User",
	Addr: Address{Country: "UK", ZipCode: "002"}, salary: 500}

err = CopyProperties(&employee2, &user)
if err != nil {
	return
}

fmt.Println(employee1)
fmt.Println(employee2)
Output:

{user001 10 Admin {CN 001} [a b] 0}
{user001 10 Admin {CN 001} [a b] 500}

func DecodeByte

func DecodeByte(data []byte, target any) error

DecodeByte decode byte slice data to target object. Play: https://go.dev/play/p/zI6xsmuQRbn

Example
var obj string
byteData := []byte{6, 12, 0, 3, 97, 98, 99}
err := DecodeByte(byteData, &obj)
if err != nil {
	return
}

fmt.Println(obj)
Output:

abc

func DeepClone

func DeepClone[T any](src T) T

DeepClone creates a deep copy of passed item. can't clone unexported field of struct Play: https://go.dev/play/p/j4DP5dquxnk

Example
type Struct struct {
	Str   string
	Int   int
	Float float64
	Bool  bool
	Nil   interface{}
	// unexported string
}

cases := []interface{}{
	true,
	1,
	0.1,
	map[string]int{
		"a": 1,
		"b": 2,
	},
	&Struct{
		Str:   "test",
		Int:   1,
		Float: 0.1,
		Bool:  true,
		Nil:   nil,
		// unexported: "can't be cloned",
	},
}

for _, item := range cases {
	cloned := DeepClone(item)

	isPointerEqual := &cloned == &item
	fmt.Println(cloned, isPointerEqual)
}
Output:

true false
1 false
0.1 false
map[a:1 b:2] false
&{test 1 0.1 true <nil>} false

func EncodeByte

func EncodeByte(data any) ([]byte, error)

EncodeByte encode data to byte slice. Play: https://go.dev/play/p/DVmM1G5JfuP

Example
byteData, _ := EncodeByte("abc")
fmt.Println(byteData)
Output:

[6 12 0 3 97 98 99]

func MapToSlice

func MapToSlice[T any, K comparable, V any](aMap map[K]V, iteratee func(K, V) T) []T

MapToSlice convert map to slice based on iteratee function. Play: https://go.dev/play/p/dmX4Ix5V6Wl

Example
aMap := map[string]int{"a": 1, "b": 2, "c": 3}
result := MapToSlice(aMap, func(key string, value int) string {
	return key + ":" + strconv.Itoa(value)
})

fmt.Println(result) //[]string{"a:1", "c:3", "b:2"} (random order)
Output:

func StructToMap

func StructToMap(value any) (map[string]any, error)

StructToMap convert struct to map, only convert exported struct field map key is specified same as struct field tag `json` value. Play: https://go.dev/play/p/KYGYJqNUBOI

Example
type People struct {
	Name string `json:"name"`
	age  int
}
p := People{
	"test",
	100,
}
pm, _ := StructToMap(p)

fmt.Println(pm)
Output:

map[name:test]

func ToBool

func ToBool(s string) (bool, error)

ToBool convert string to boolean. Play: https://go.dev/play/p/ARht2WnGdIN

Example
cases := []string{"1", "true", "True", "false", "False", "0", "123", "0.0", "abc"}

for i := 0; i < len(cases); i++ {
	result, _ := ToBool(cases[i])
	fmt.Println(result)
}
Output:

true
true
true
false
false
false
false
false
false

func ToBytes

func ToBytes(value any) ([]byte, error)

ToBytes convert value to byte slice. Play: https://go.dev/play/p/fAMXYFDvOvr

Example
result1, _ := ToBytes(1)
result2, _ := ToBytes("abc")
result3, _ := ToBytes(true)

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
Output:

[0 0 0 0 0 0 0 1]
[97 98 99]
[116 114 117 101]

func ToChannel

func ToChannel[T any](array []T) <-chan T

ToChannel convert a slice of elements to a read-only channel. Play: https://go.dev/play/p/hOx_oYZbAnL

Example
ch := ToChannel([]int{1, 2, 3})
result1 := <-ch
result2 := <-ch
result3 := <-ch

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
Output:

1
2
3

func ToChar

func ToChar(s string) []string

ToChar convert string to char slice. Play: https://go.dev/play/p/JJ1SvbFkVdM

Example
result1 := ToChar("")
result2 := ToChar("abc")
result3 := ToChar("1 2#3")

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
Output:

[]
[a b c]
[1   2 # 3]

func ToFloat

func ToFloat(value any) (float64, error)

ToFloat convert value to float64, if input is not a float return 0.0 and error. Play: https://go.dev/play/p/4YTmPCibqHJ

Example
result1, _ := ToFloat("")
result2, _ := ToFloat("abc")
result3, _ := ToFloat("-1")
result4, _ := ToFloat("-.11")
result5, _ := ToFloat("1.23e3")
result6, _ := ToFloat(true)

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
fmt.Println(result6)
Output:

0
0
-1
-0.11
1230
0

func ToInt

func ToInt(value any) (int64, error)

ToInt convert value to int64 value, if input is not numerical, return 0 and error. Play: https://go.dev/play/p/9_h9vIt-QZ_b

Example
result1, _ := ToInt("123")
result2, _ := ToInt("-123")
result3, _ := ToInt(float64(12.3))
result4, _ := ToInt("abc")
result5, _ := ToInt(true)

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
Output:

123
-123
12
0
0

func ToJson

func ToJson(value any) (string, error)

ToJson convert value to a json string. Play: https://go.dev/play/p/2rLIkMmXWvR

Example
aMap := map[string]int{"a": 1, "b": 2, "c": 3}
result1, err := ToJson(aMap)
if err != nil {
	fmt.Printf("%v", err)
}
fmt.Println(result1)
Output:

{"a":1,"b":2,"c":3}

func ToMap

func ToMap[T any, K comparable, V any](array []T, iteratee func(T) (K, V)) map[K]V

ToMap convert a slice of structs to a map based on iteratee function. Play: https://go.dev/play/p/tVFy7E-t24l

Example
type Message struct {
	name string
	code int
}
messages := []Message{
	{name: "Hello", code: 100},
	{name: "Hi", code: 101},
}
result := ToMap(messages, func(msg Message) (int, string) {
	return msg.code, msg.name
})

fmt.Println(result)
Output:

map[100:Hello 101:Hi]

func ToPointer

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

ToPointer returns a pointer to passed value. Play: https://go.dev/play/p/ASf_etHNlw1

Example
result := ToPointer(123)
fmt.Println(*result)
Output:

123

func ToString

func ToString(value any) string

ToString convert value to string for number, string, []byte, will convert to string for other type (slice, map, array, struct) will call json.Marshal. Play: https://go.dev/play/p/nF1zOOslpQq

Example
result1 := ToString("")
result2 := ToString(nil)
result3 := ToString(0)
result4 := ToString(1.23)
result5 := ToString(true)
result6 := ToString(false)
result7 := ToString([]int{1, 2, 3})

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
fmt.Println(result6)
fmt.Println(result7)
Output:


0
1.23
true
false
[1,2,3]

Types

This section is empty.

Jump to

Keyboard shortcuts

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