# Convertor

convertor 转换器包支持一些常见的数据类型转换

# 源码:

# 用法:

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

# 文档

# ColorHexToRGB

颜色值十六进制转rgb

函数签名:

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

列子:

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

颜色值rgb转十六进制

函数签名:

func ColorRGBToHex(red, green, blue int) string

列子:

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

字符串转布尔类型,使用strconv.ParseBool

函数签名:

func ToBool(s string) (bool, error)

列子:

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

interface转字节切片.

函数签名:

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

列子:

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

字符串转字符切片

函数签名:

func ToChar(s string) []string

列子:

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

将切片转为只读channel

函数签名:

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

例子:

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

将interface转成float64类型,如果参数无法转换,会返回0和error

函数签名:

func ToFloat(value any) (float64, error)

列子:

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

将interface转成int64类型,如果参数无法转换,会返回0和error

函数签名:

func ToInt(value any) (int64, error)

例子:

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

将interface转成json字符串,如果参数无法转换,会返回""和error

函数签名:

func ToJson(value any) (string, error)

列子:

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

将切片转为map

函数签名:

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

例子:

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

返回传入值的指针

函数签名:

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

例子:

package main

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

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

# ToString

将interface转成字符串

函数签名:

func ToString(value any) string

例子:

package main

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

func main() {
    fmt.Printf("%q", convertor.ToString(1)) //"1"
    fmt.Printf("%q", convertor.ToString(1.1)) //"1.1"
    fmt.Printf("%q", convertor.ToString([]int{1, 2, 3})) //"[1,2,3]"
}

# StructToMap

将struct转成map,只会转换struct中可导出的字段。struct中导出字段需要设置json tag标记

函数签名:

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

列子:

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

map中key和value执行函数iteratee后,转为切片

函数签名:

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

例子:

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

将data编码成字节切片

函数签名:

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

例子:

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

解码字节切片到目标对象,目标对象需要传入一个指针实例子

函数签名:

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

例子:

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"
}
最后更新时间: 2022/9/29 下午1:53:14