Back to godoc.org

Package unsafe

v0.0.9
Latest Go to latest
Published: Jun 24, 2020 | License: Apache-2.0 | Module: github.com/grailbio/base

Index

Examples

Package Files

func BytesToString

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

Example

Code:

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

func ExtendBytes

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).

Example

Code:

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

func StringToBytes

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

Example

Code:

fmt.Println(unsafe.StringToBytes("AbC"))
[65 98 67]
Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier