Skip to content

Latest commit

 

History

History
32 lines (25 loc) · 753 Bytes

README.md

File metadata and controls

32 lines (25 loc) · 753 Bytes

XUnsafe

在go里面, []bytestring 的相互转换是非常频繁的,随之而来的是大量的内存分配,当长度越长时,对性能影响越大,所有提供了 unsafe 的转换方法。

[]byte 是由 string 通过 unsafe 转换而来的话,将不能进行更改操作,否则会panic,且无法恢复。

StringToBytes

package main
import (
    "github.com/go-board/x-go/xunsafe"
)
func main()  {
    bs := xunsafe.StringToBytes("hello,world!")
    println(bs)
}

BytesToString

package main
import (
    "github.com/go-board/x-go/xunsafe"
)

func main()  {
    str := xunsafe.BytesToString([]byte{'h', 'e', 'l', 'l', 'o', ',', 'w', 'o', 'r', 'l', 'd', '!'})
    println(str)
}