base: github.com/grailbio/base/unsafe Index | Examples | Files

package unsafe

import "github.com/grailbio/base/unsafe"

Index

Examples

Package Files

unsafe.go

func BytesToString Uses

func BytesToString(src []byte) (d string)

BytesToString casts src to a string without extra memory allocation. The string returned by this function shares memory with "src".

Code:

fmt.Println(unsafe.BytesToString([]byte{'A', 'b', 'C'}))

Output:

AbC

func ExtendBytes Uses

func ExtendBytes(dptr *[]byte, newLen int)

ExtendBytes extends the given byte slice, without zero-initializing the new storage space. The caller must guarantee that cap(d) >= newLen (using e.g. a Grow() call on the parent buffer).

Code:

d := []byte{'A', 'b', 'C'}
d = d[:1]
unsafe.ExtendBytes(&d, 2)
fmt.Println(d)

Output:

[65 98]

func StringToBytes Uses

func StringToBytes(src string) (d []byte)

StringToBytes casts src to []byte without extra memory allocation. The data returned by this function shares memory with "src".

Code:

fmt.Println(unsafe.StringToBytes("AbC"))

Output:

[65 98 67]

Package unsafe imports 2 packages (graph) and is imported by 12 packages. Updated 2018-08-13. Refresh now. Tools for package owners.