Skip to content
On this page

# Convertor ​

Package convertor contains some functions for data type convertion.

## Source: ​

- https://github.com/duke-git/lancet/blob/main/convertor/convertor.go

## Usage: ​

go

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

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

## Index ​

- ColorHexToRGB
- ColorRGBToHex
- ToBool
- ToBytes
- ToChar
- ToChannel
- ToFloat
- ToInt
- ToJson
- ToMap
- ToPointer
- ToString
- StructToMap
- MapToSlice
- EncodeByte
- DecodeByte
- DeepClone
- CopyProperties
- ToInterface
- Utf8ToGbk
- GbkToUtf8

## Documentation ​

### ColorHexToRGB ​

Convert color hex to color rgb.

Signature:

go

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

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

Example:Run

go

    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)

        // Output:
        // 0 51 102
    }

    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)

        // Output:
        // 0 51 102
    }

### ColorRGBToHex ​

Convert color rgb to color hex.

Signature:

go

    func ColorRGBToHex(red, green, blue int) string

    func ColorRGBToHex(red, green, blue int) string

Example:Run

go

    package main

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

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

        fmt.Println(colorHex)

        // Output:
        // #003366
    }

    package main

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

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

        fmt.Println(colorHex)

        // Output:
        // #003366
    }

### ToBool ​

Convert string to bool. Use strconv.ParseBool.

Signature:

go

    func ToBool(s string) (bool, error)

    func ToBool(s string) (bool, error)

Example:Run

go

    package main

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

    func main() {
        cases := []string{"1", "true", "True", "false", "False", "0", "123", "0.0", "abc"}

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

        // Output:
        // true
        // true
        // true
        // false
        // false
        // false
        // false
        // false
        // false
    }

    package main

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

    func main() {
        cases := []string{"1", "true", "True", "false", "False", "0", "123", "0.0", "abc"}

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

        // Output:
        // true
        // true
        // true
        // false
        // false
        // false
        // false
        // false
        // false
    }

### ToBytes ​

Convert value to byte slice.

Signature:

go

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

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

Example:Run

go

    package main

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

    func main() {
        bytesData, err := convertor.ToBytes("abc")
        if err != nil {
            fmt.Println(err)
        }

        fmt.Println(bytesData)

        // Output:
        // [97 98 99]
    }

    package main

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

    func main() {
        bytesData, err := convertor.ToBytes("abc")
        if err != nil {
            fmt.Println(err)
        }

        fmt.Println(bytesData)

        // Output:
        // [97 98 99]
    }

### ToChar ​

Convert string to char slice.

Signature:

go

    func ToChar(s string) []string

    func ToChar(s string) []string

Example:Run

go

    package main

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

    func main() {
        result1 := convertor.ToChar("")
        result2 := convertor.ToChar("abc")
        result3 := convertor.ToChar("1 2#3")

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

        // Output:
        // []
        // [a b c]
        // [1   2 # 3]
    }

    package main

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

    func main() {
        result1 := convertor.ToChar("")
        result2 := convertor.ToChar("abc")
        result3 := convertor.ToChar("1 2#3")

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

        // Output:
        // []
        // [a b c]
        // [1   2 # 3]
    }

### ToChannel ​

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

Signature:

go

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

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

Example:Run

go

    package main

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

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

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

        // Output:
        // 1
        // 2
        // 3
    }

    package main

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

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

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

        // Output:
        // 1
        // 2
        // 3
    }

### ToFloat ​

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

Signature:

go

    func ToFloat(value any) (float64, error)

    func ToFloat(value any) (float64, error)

Example:Run

go

    package main

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

    func main() {
        result1, _ := convertor.ToFloat("")
        result2, err := convertor.ToFloat("abc")
        result3, _ := convertor.ToFloat("-1")
        result4, _ := convertor.ToFloat("-.11")
        result5, _ := convertor.ToFloat("1.23e3")
        result6, _ := convertor.ToFloat(true)

        fmt.Println(result1)
        fmt.Println(result2, err)
        fmt.Println(result3)
        fmt.Println(result4)
        fmt.Println(result5)
        fmt.Println(result6)

        // Output:
        // 0
        // 0 strconv.ParseFloat: parsing "": invalid syntax
        // -1
        // -0.11
        // 1230
        // 0
    }

    package main

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

    func main() {
        result1, _ := convertor.ToFloat("")
        result2, err := convertor.ToFloat("abc")
        result3, _ := convertor.ToFloat("-1")
        result4, _ := convertor.ToFloat("-.11")
        result5, _ := convertor.ToFloat("1.23e3")
        result6, _ := convertor.ToFloat(true)

        fmt.Println(result1)
        fmt.Println(result2, err)
        fmt.Println(result3)
        fmt.Println(result4)
        fmt.Println(result5)
        fmt.Println(result6)

        // Output:
        // 0
        // 0 strconv.ParseFloat: parsing "": invalid syntax
        // -1
        // -0.11
        // 1230
        // 0
    }

### ToInt ​

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

Signature:

go

    func ToInt(value any) (int64, error)

    func ToInt(value any) (int64, error)

Example:Run

go

    package main

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

    func main() {
        result1, _ := convertor.ToInt("123")
        result2, _ := convertor.ToInt("-123")
        result3, _ := convertor.ToInt(float64(12.3))
        result4, err := convertor.ToInt("abc")
        result5, _ := convertor.ToInt(true)

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4, err)
        fmt.Println(result5)

        // Output:
        // 123
        // -123
        // 12
        // 0 strconv.ParseInt: parsing "": invalid syntax
        // 0
    }

    package main

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

    func main() {
        result1, _ := convertor.ToInt("123")
        result2, _ := convertor.ToInt("-123")
        result3, _ := convertor.ToInt(float64(12.3))
        result4, err := convertor.ToInt("abc")
        result5, _ := convertor.ToInt(true)

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4, err)
        fmt.Println(result5)

        // Output:
        // 123
        // -123
        // 12
        // 0 strconv.ParseInt: parsing "": invalid syntax
        // 0
    }

### ToJson ​

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

Signature:

go

    func ToJson(value any) (string, error)

    func ToJson(value any) (string, error)

Example:Run

go

    package main

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

    func main() {
        aMap := map[string]int{"a": 1, "b": 2, "c": 3}
        result, err := ToJson(aMap)

        if err != nil {
            fmt.Printf("%v", err)
        }

        fmt.Println(result)

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

    package main

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

    func main() {
        aMap := map[string]int{"a": 1, "b": 2, "c": 3}
        result, err := ToJson(aMap)

        if err != nil {
            fmt.Printf("%v", err)
        }

        fmt.Println(result)

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

### ToMap ​

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

Signature:

go

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

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

Example:Run

go

    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)

        // Output:
        // map[100:Hello 101:Hi]
    }

    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)

        // Output:
        // map[100:Hello 101:Hi]
    }

### ToPointer ​

Returns a pointer to passed value.

Signature:

go

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

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

Example:Run

go

    package main

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

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

        // Output:
        // 123
    }

    package main

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

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

        // Output:
        // 123
    }

### ToString ​

ToString convert value to string, for number, string, []byte, will convert to string. For other type (slice, map, array, struct) will call json.Marshal

Signature:

go

    func ToString(value any) string

    func ToString(value any) string

Example:Run

go

    package main

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

    func main() {
        result1 := convertor.ToString("")
        result2 := convertor.ToString(nil)
        result3 := convertor.ToString(0)
        result4 := convertor.ToString(1.23)
        result5 := convertor.ToString(true)
        result6 := convertor.ToString(false)
        result7 := convertor.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]
    }

    package main

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

    func main() {
        result1 := convertor.ToString("")
        result2 := convertor.ToString(nil)
        result3 := convertor.ToString(0)
        result4 := convertor.ToString(1.23)
        result5 := convertor.ToString(true)
        result6 := convertor.ToString(false)
        result7 := convertor.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]
    }

### StructToMap ​

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

Signature:

go

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

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

Example:Run

go

    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.Println(pm)

        // Output:
        // map[name:test]
    }

    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.Println(pm)

        // Output:
        // map[name:test]
    }

### MapToSlice ​

Convert a map to a slice based on iteratee function.

Signature:

go

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

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

Example:Run

go

    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"}
    }

    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:

go

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

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

Example:Run

go

    package main

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

    func main() {
        byteData, _ := convertor.EncodeByte("abc")
        fmt.Println(byteData)

        // Output:
        // [6 12 0 3 97 98 99]
    }

    package main

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

    func main() {
        byteData, _ := convertor.EncodeByte("abc")
        fmt.Println(byteData)

        // Output:
        // [6 12 0 3 97 98 99]
    }

### DecodeByte ​

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

Signature:

go

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

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

Example:Run

go

    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}

        err := convertor.DecodeByte(byteData, &result)
        if err != nil {
            return
        }

        fmt.Println(result)

        // Output:
        // abc
    }

    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}

        err := convertor.DecodeByte(byteData, &result)
        if err != nil {
            return
        }

        fmt.Println(result)

        // Output:
        // abc
    }

### DeepClone ​

Creates a deep copy of passed item, can't clone unexported field of struct.

Signature:

go

    func DeepClone[T any](src T) T

    func DeepClone[T any](src T) T

Example:Run

go

    package main

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

    func main() {
        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 := convertor.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
    }

    package main

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

    func main() {
        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 := convertor.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
    }

### CopyProperties ​

Copies each field from the source struct into the destination struct. Use json.Marshal/Unmarshal, so json tag should be set for fields of dst and src struct.

Signature:

go

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

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

Example:Run

go

    package main

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

    func main() {
        type Disk struct {
            Name    string  `json:"name"`
            Total   string  `json:"total"`
            Used    string  `json:"used"`
            Percent float64 `json:"percent"`
        }

        type DiskVO struct {
            Name    string  `json:"name"`
            Total   string  `json:"total"`
            Used    string  `json:"used"`
            Percent float64 `json:"percent"`
        }

        type Indicator struct {
            Id      string    `json:"id"`
            Ip      string    `json:"ip"`
            UpTime  string    `json:"upTime"`
            LoadAvg string    `json:"loadAvg"`
            Cpu     int       `json:"cpu"`
            Disk    []Disk    `json:"disk"`
            Stop    chan bool `json:"-"`
        }

        type IndicatorVO struct {
            Id      string   `json:"id"`
            Ip      string   `json:"ip"`
            UpTime  string   `json:"upTime"`
            LoadAvg string   `json:"loadAvg"`
            Cpu     int64    `json:"cpu"`
            Disk    []DiskVO `json:"disk"`
        }

        indicator := &Indicator{Id: "001", Ip: "127.0.0.1", Cpu: 1, Disk: []Disk{
            {Name: "disk-001", Total: "100", Used: "1", Percent: 10},
            {Name: "disk-002", Total: "200", Used: "1", Percent: 20},
            {Name: "disk-003", Total: "300", Used: "1", Percent: 30},
        }}

        indicatorVO := IndicatorVO{}

        err := convertor.CopyProperties(&indicatorVO, indicator)

        if err != nil {
            return
        }

        fmt.Println(indicatorVO.Id)
        fmt.Println(indicatorVO.Ip)
        fmt.Println(len(indicatorVO.Disk))

        // Output:
        // 001
        // 127.0.0.1
        // 3
    }

    package main

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

    func main() {
        type Disk struct {
            Name    string  `json:"name"`
            Total   string  `json:"total"`
            Used    string  `json:"used"`
            Percent float64 `json:"percent"`
        }

        type DiskVO struct {
            Name    string  `json:"name"`
            Total   string  `json:"total"`
            Used    string  `json:"used"`
            Percent float64 `json:"percent"`
        }

        type Indicator struct {
            Id      string    `json:"id"`
            Ip      string    `json:"ip"`
            UpTime  string    `json:"upTime"`
            LoadAvg string    `json:"loadAvg"`
            Cpu     int       `json:"cpu"`
            Disk    []Disk    `json:"disk"`
            Stop    chan bool `json:"-"`
        }

        type IndicatorVO struct {
            Id      string   `json:"id"`
            Ip      string   `json:"ip"`
            UpTime  string   `json:"upTime"`
            LoadAvg string   `json:"loadAvg"`
            Cpu     int64    `json:"cpu"`
            Disk    []DiskVO `json:"disk"`
        }

        indicator := &Indicator{Id: "001", Ip: "127.0.0.1", Cpu: 1, Disk: []Disk{
            {Name: "disk-001", Total: "100", Used: "1", Percent: 10},
            {Name: "disk-002", Total: "200", Used: "1", Percent: 20},
            {Name: "disk-003", Total: "300", Used: "1", Percent: 30},
        }}

        indicatorVO := IndicatorVO{}

        err := convertor.CopyProperties(&indicatorVO, indicator)

        if err != nil {
            return
        }

        fmt.Println(indicatorVO.Id)
        fmt.Println(indicatorVO.Ip)
        fmt.Println(len(indicatorVO.Disk))

        // Output:
        // 001
        // 127.0.0.1
        // 3
    }

### ToInterface ​

Converts reflect value to its interface type.

Signature:

go

    func ToInterface(v reflect.Value) (value interface{}, ok bool)

    func ToInterface(v reflect.Value) (value interface{}, ok bool)

Example:Run

go

    package main

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

    func main() {
        val := reflect.ValueOf("abc")
        iVal, ok := convertor.ToInterface(val)

        fmt.Printf("%T\n", iVal)
        fmt.Printf("%v\n", iVal)
        fmt.Println(ok)

        // Output:
        // string
        // abc
        // true
    }

    package main

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

    func main() {
        val := reflect.ValueOf("abc")
        iVal, ok := convertor.ToInterface(val)

        fmt.Printf("%T\n", iVal)
        fmt.Printf("%v\n", iVal)
        fmt.Println(ok)

        // Output:
        // string
        // abc
        // true
    }

### Utf8ToGbk ​

Converts utf8 encoding data to GBK encoding data.

Signature:

go

    func Utf8ToGbk(bs []byte) ([]byte, error)

    func Utf8ToGbk(bs []byte) ([]byte, error)

Example:Run

go

    package main

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

    func main() {
        utf8Data := []byte("hello")
        gbkData, _ := convertor.Utf8ToGbk(utf8Data)

        fmt.Println(utf8.Valid(utf8Data))
        fmt.Println(validator.IsGBK(gbkData))

        // Output:
        // true
        // true
    }

    package main

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

    func main() {
        utf8Data := []byte("hello")
        gbkData, _ := convertor.Utf8ToGbk(utf8Data)

        fmt.Println(utf8.Valid(utf8Data))
        fmt.Println(validator.IsGBK(gbkData))

        // Output:
        // true
        // true
    }

### GbkToUtf8 ​

Converts GBK encoding data to utf8 encoding data.

Signature:

go

    func GbkToUtf8(bs []byte) ([]byte, error)

    func GbkToUtf8(bs []byte) ([]byte, error)

Example:Run

go

    package main

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

    func main() {
        gbkData, _ := convertor.Utf8ToGbk([]byte("hello"))
        utf8Data, _ := convertor.GbkToUtf8(gbkData)

        fmt.Println(utf8.Valid(utf8Data))
        fmt.Println(string(utf8Data))

        // Output:
        // true
        // hello
    }

    package main

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

    func main() {
        gbkData, _ := convertor.Utf8ToGbk([]byte("hello"))
        utf8Data, _ := convertor.GbkToUtf8(gbkData)

        fmt.Println(utf8.Valid(utf8Data))
        fmt.Println(string(utf8Data))

        // Output:
        // true
        // hello
    }