cast

package module
v0.0.0-...-2064725 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2020 License: MIT Imports: 2 Imported by: 2

README

CAST

GoDoc

Quickly convert string <-> []byte without memory reallocations and create mutable string or immutable []byte.

This package is a danger zone and should not be entered without understanding the ground rules.

  1. Converting a string -> []byte will result in an immutable byte slice. Editing will cause a panic.
  2. Converting a []byte -> string will result in a mutable string. Editing the originial bytes will change the string too.

Create immutable []byte:

var s = "Hello Planet"
b := cast.ToBytes(s)
fmt.Printf("%s\n", "J"+string(b[1:]))

// Output:
// Jello Planet

Create mutable string:

var b = []byte("Hello Planet")
s := cast.ToString(b)
b[0] = 'J'
fmt.Printf("%s\n", s)

// Output:
// Jello Planet

Contact

Josh Baker @tidwall

License

CAST source code is available under the MIT License.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToBytes

func ToBytes(s string) []byte

ToBytes will quickly return a []byte representation of a string without allocating new memory or having to copy data. Converting a string -> []byte will result in an immutable byte slice. Editing will cause a panic.

Example
var s = "Hello Planet"
b := ToBytes(s)
fmt.Printf("%s\n", "J"+string(b[1:]))
Output:

Jello Planet

func ToString

func ToString(b []byte) string

ToString will quickly return a string representation of a []byte without allocating new memory or having to copy data. Converting a []byte -> string will result in a mutable string. Editing the originial bytes will change the string too.

Example
var b = []byte("Hello Planet")
s := ToString(b)
b[0] = 'J'
fmt.Printf("%s\n", s)
Output:

Jello Planet

Types

This section is empty.

Jump to

Keyboard shortcuts

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