# Convertor

Package convertor contains some functions for data type convertion.

# Source:

# Usage:

import (
    "github.com/duke-git/lancet/v2/convertor"
)

# Documentation

# ColorHexToRGB

Convert color hex to color rgb.

Signature:

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

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/convertor"
)

func main() {
    colorHex := "#003366"
    r, g, b := convertor.ColorHexToRGB(colorHex)
    fmt.Println(r, g, b) //0,51,102
}

# ColorRGBToHex

Convert color rgb to color hex.

Signature:

func ColorRGBToHex(red, green, blue int) string

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/convertor"
)

func main() {
    r := 0
    g := 51
    b := 102
    colorHex := convertor.ColorRGBToHex(r, g, b)

    fmt.Println(colorHex) //#003366
}

# ToBool

Convert string to a boolean value. Use strconv.ParseBool

Signature:

func ToBool(s string) (bool, error)

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/convertor"
)

func main() {
    v1, _ := convertor.ToBool("1")
    fmt.Println(v1) //true

    v2, _ := convertor.ToBool("true")
    fmt.Println(v2) //true

    v3, _ := convertor.ToBool("True")
    fmt.Println(v3) //true

    v4, _ := convertor.ToBool("123")
    fmt.Println(v4) //false
}

# ToBytes

Convert interface to byte slice.

Signature:

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

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/convertor"
)

func main() {
    bytesData, err := convertor.ToBytes("0")
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(bytesData) //[]bytes{3, 4, 0, 0}
}

# ToChar

Convert string to char slice.

Signature:

func ToChar(s string) []string

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/convertor"
)

func main() {
    chars := convertor.ToChar("")
    fmt.Println(chars) //[]string{""}

    chars = convertor.ToChar("abc")
    fmt.Println(chars) //[]string{"a", "b", "c"}

    chars = convertor.ToChar("1 2#3")
    fmt.Println(chars) //[]string{"1", " ", "2", "#", "3"}
}

# ToChannel

Convert a collection of elements to a read-only channels.

Signature:

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

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/convertor"
)

func main() {
    ch := convertor.ToChannel([]int{1, 2, 3})

    val1, _ := <-ch
    fmt.Println(val1) //1

    val2, _ := <-ch
    fmt.Println(val2) //2

    val3, _ := <-ch
    fmt.Println(val3) //3

    _, ok := <-ch
    fmt.Println(ok) //false
}

# ToFloat

Convert interface to a float64 value. If param is a invalid floatable, will return 0 and error.

Signature:

func ToFloat(value any) (float64, error)

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/convertor"
)

func main() {
    v, err := convertor.ToFloat("")
    if err != nil {
        fmt.Println(err) //strconv.ParseFloat: parsing "": invalid syntax
    }
    fmt.Println(v) //0

    v, _ = convertor.ToFloat("-.11")
    fmt.Println(v) //-0.11
}

# ToInt

Convert interface to a int64 value. If param is a invalid intable, will return 0 and error.

Signature:

func ToInt(value any) (int64, error)

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/convertor"
)

func main() {
    v, err := convertor.ToInt("")
    if err != nil {
        fmt.Println(err) //strconv.ParseInt: parsing "": invalid syntax
    }
    fmt.Println(v) //0

    v, _ = convertor.ToFloat(1.12)
    fmt.Println(v) //1
}

# ToJson

Convert interface to json string. If param can't be converted, will return "" and error.

Signature:

func ToJson(value any) (string, error)

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/convertor"
)

func main() {
    var aMap = map[string]int{"a": 1, "b": 2, "c": 3}
    jsonStr, _ := convertor.ToJson(aMap)
    fmt.Printf("%q", jsonStr) //"{\"a\":1,\"b\":2,\"c\":3}"
}

# ToMap

Convert a slice or an array of structs to a map based on iteratee function.

Signature:

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

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/convertor"
)

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

    fmt.Println(result) //{100: "Hello", 101: "Hi"}
}

# ToPointer

Returns a pointer to passed value.

Signature:

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

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/convertor"
)

func main() {
    result := convertor.ToPointer(123)
    fmt.Println(*result) //123
}

# StructToMap

Convert struct to map, only convert exported field, struct field tag `json` should be set.

Signature:

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

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/convertor"
)

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

    fmt.Printf("type: %T, value: %s", pm, pm) //type: map[string]interface {}, value: map[name:test]
}

# MapToSlice

Convert a map to a slice based on iteratee function.

Signature:

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

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/convertor"
)

func main() {
    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", "b:2", "c:3"}
}

# EncodeByte

Encode data to byte slice.

Signature:

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

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/convertor"
)

func main() {
    byteData, _ := convertor.EncodeByte("abc")
    fmt.Println(byteData) //[]byte{6, 12, 0, 3, 97, 98, 99}
}

# DecodeByte

Decode byte data to target object. target should be a pointer instance.

Signature:

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

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/convertor"
)

func main() {
    var result string
	byteData := []byte{6, 12, 0, 3, 97, 98, 99}
	convertor.DecodeByte(byteData, &result)
    fmt.Println(result) //"abc"
}
Last Updated: 9/29/2022, 1:53:14 PM