Standard library / unsafe

unsafe.StringData()

Obtains a pointer to the underlying bytes of a string. Treat the pointer as read-only; never modify string storage. Empty strings may return nil or another unspecified address, so do not dereference them. A byte is not necessarily a complete Unicode character. Signatures use documentation-only placeholders where applicable; these are compiler intrinsics, not ordinary function values. Expected stdout: '71\n'.

Formal Syntax & Structure

go syntaxgo
func StringData(str string) *byte

Parameters & Attributes

NameTypeDescription
strstringString whose first byte address is requested.

Return Value

*byte

Read-only-by-contract pointer to string bytes; unspecified for empty strings.

Working Example

Example Snippetgo
package main
import (  "fmt"  "runtime"  "unsafe")
func main() {  s := "Go"  if len(s) > 0 {    fmt.Println(*unsafe.StringData(s))  }  runtime.KeepAlive(s)}
Go Reference