mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-15 02:02:27 +08:00
doc: add doc for Utf8ToGbk and GbkToUtf8
This commit is contained in:
@@ -379,7 +379,7 @@ func ToInterface(v reflect.Value) (value interface{}, ok bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Utf8ToGbk convert utf8 encoding to GBK encoding.
|
// Utf8ToGbk convert utf8 encoding data to GBK encoding data.
|
||||||
// Play: todo
|
// Play: todo
|
||||||
func Utf8ToGbk(bs []byte) ([]byte, error) {
|
func Utf8ToGbk(bs []byte) ([]byte, error) {
|
||||||
r := transform.NewReader(bytes.NewReader(bs), simplifiedchinese.GBK.NewEncoder())
|
r := transform.NewReader(bytes.NewReader(bs), simplifiedchinese.GBK.NewEncoder())
|
||||||
@@ -387,7 +387,7 @@ func Utf8ToGbk(bs []byte) ([]byte, error) {
|
|||||||
return b, err
|
return b, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// GbkToUtf8 convert GBK encoding to utf8 encoding.
|
// GbkToUtf8 convert GBK encoding data to utf8 encoding data.
|
||||||
// Play: todo
|
// Play: todo
|
||||||
func GbkToUtf8(bs []byte) ([]byte, error) {
|
func GbkToUtf8(bs []byte) ([]byte, error) {
|
||||||
r := transform.NewReader(bytes.NewReader(bs), simplifiedchinese.GBK.NewDecoder())
|
r := transform.NewReader(bytes.NewReader(bs), simplifiedchinese.GBK.NewDecoder())
|
||||||
|
|||||||
@@ -41,6 +41,8 @@ import (
|
|||||||
- [DeepClone](#DeepClone)
|
- [DeepClone](#DeepClone)
|
||||||
- [CopyProperties](#CopyProperties)
|
- [CopyProperties](#CopyProperties)
|
||||||
- [ToInterface](#ToInterface)
|
- [ToInterface](#ToInterface)
|
||||||
|
- [Utf8ToGbk](#Utf8ToGbk)
|
||||||
|
- [GbkToUtf8](#GbkToUtf8)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -795,15 +797,82 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
val := reflect.ValueOf("abc")
|
val := reflect.ValueOf("abc")
|
||||||
iVal, ok := convertor.ToInterface(val)
|
iVal, ok := convertor.ToInterface(val)
|
||||||
|
|
||||||
fmt.Printf("%T\n", iVal)
|
fmt.Printf("%T\n", iVal)
|
||||||
fmt.Printf("%v\n", iVal)
|
fmt.Printf("%v\n", iVal)
|
||||||
fmt.Println(ok)
|
fmt.Println(ok)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// string
|
// string
|
||||||
// abc
|
// abc
|
||||||
// true
|
// true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="Utf8ToGbk">Utf8ToGbk</span>
|
||||||
|
|
||||||
|
<p>Converts utf8 encoding data to GBK encoding data.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Utf8ToGbk(bs []byte) ([]byte, error)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="GbkToUtf8">GbkToUtf8</span>
|
||||||
|
|
||||||
|
<p>Converts GBK encoding data to utf8 encoding data.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func GbkToUtf8(bs []byte) ([]byte, error)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```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
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -795,15 +795,82 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
val := reflect.ValueOf("abc")
|
val := reflect.ValueOf("abc")
|
||||||
iVal, ok := convertor.ToInterface(val)
|
iVal, ok := convertor.ToInterface(val)
|
||||||
|
|
||||||
fmt.Printf("%T\n", iVal)
|
fmt.Printf("%T\n", iVal)
|
||||||
fmt.Printf("%v\n", iVal)
|
fmt.Printf("%v\n", iVal)
|
||||||
fmt.Println(ok)
|
fmt.Println(ok)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// string
|
// string
|
||||||
// abc
|
// abc
|
||||||
// true
|
// true
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="Utf8ToGbk">Utf8ToGbk</span>
|
||||||
|
|
||||||
|
<p>utf8编码转GBK编码。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Utf8ToGbk(bs []byte) ([]byte, error)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="GbkToUtf8">GbkToUtf8</span>
|
||||||
|
|
||||||
|
<p>GBK编码转utf8编码。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func GbkToUtf8(bs []byte) ([]byte, error)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```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
|
||||||
|
}
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user