xunsafe

package
v0.0.0-...-f89cf20 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 8, 2020 License: MIT Imports: 2 Imported by: 0

README

XUnsafe

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

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

StringToBytes

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

BytesToString

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BytesToString

func BytesToString(data []byte) string

BytesToString use a quick way to cast []byte to string without any alloc, in a 6000 length test, this is faster than `string(bytes)` one 2200 times

func StringToBytes

func StringToBytes(str string) []byte

StringToBytes use a quick way to cast string to []byte without any alloc, in a 6000 length test, this is faster than `[]byte(string)` one 2200 times CAUTION: this is marked as unsafe so have limit, do not change the result.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL