Standard library / unsafe

unsafe.String()

Builds a string over existing bytes without copying. The full range must be readable and remain immutable while the string exists. Use string(b) for ordinary conversions and ownership independence. Nil with zero length produces an empty string; nil with nonzero length or a negative runtime length panics. Signatures use documentation-only placeholders where applicable; these are compiler intrinsics, not ordinary function values. Expected stdout: 'Go\n'.

Formal Syntax & Structure

go syntaxgo
func String(ptr *byte, len IntegerType) string

Parameters & Attributes

NameTypeDescription
ptr*bytePointer to live bytes that will not be modified while the string exists.
lenIntegerTypeNonnegative byte count within the readable allocation.

Return Value

string

String sharing the specified immutable bytes.

Working Example

Example Snippetgo
package main
import (  "fmt"  "runtime"  "unsafe")
func main() {  b := []byte{'G', 'o'}  s := unsafe.String(unsafe.SliceData(b), len(b))  fmt.Println(s)  runtime.KeepAlive(b)}
Go Reference