unsafe.String and unsafe.StringData

January 28, 2025

Package unsafe

The function String returns a string value whose underlying bytes start at ptr and whose length is len. The same requirements apply to the ptr and len argument as in the function Slice. If len is zero, the result is the empty string "". Since Go strings are immutable, the bytes passed to String must not be modified afterwards. [Go 1.20]

The function StringData returns a pointer to the underlying bytes of the str argument. For an empty string the return value is unspecified, and may be nil. Since Go strings are immutable, the bytes returned by StringData must not be modified [Go 1.20].

As mentioned twice above, and also discussed many times before, strings in Go are immutable. This means that making changes to strings requires making in-memory copies, and this can be inefficient.

This is why these two functions are sometimes used when high performance is essential, and if you’re willing to violate the string immutability rule of Go. This is package unsafe, after all!

It’s not advised to do this, however. And I’m not even going to show you how.

Instead, I’ll suggest, if you’re working on performance critical code, and string modification is your bottleneck, almost always, there are better approaches, such as:

  • Use []byte instead (and the bytes package, instead of the strings package for common maninpulations)
  • Ues a strings.Builder

Quotes from The Go Programming Language Specification Language version go1.23 (June 13, 2024)


Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .

Related Content


String concatenation

Today we’re looking at a deceptively short section of the spec: String concatenation… String concatenation Strings can be concatenated using the + operator or the += assignment operator: s := "hi" + string(c) s += " and good bye" String addition creates a new string by concatenating the operands. That’s it. Short, and sweet, eh? Except that it’s not quite so sweet, when you consider the implications of the last sentence: Every time you use + or += for a string, you create a new string.


unsafe.Slice

Package unsafe … The function Slice returns a slice whose underlying array starts at ptr and whose length and capacity are len. Slice(ptr, len) is equivalent to (*[len]ArbitraryType)(unsafe.Pointer(ptr))[:] except that, as a special case, if ptr is nil and len is zero, Slice returns nil [Go 1.17]. The len argument must be of integer type or an untyped constant. A constant len argument must be non-negative and representable by a value of type int; if it is an untyped constant it is given type int.


Pointer Arithmetic

I mentioned a few days ago that pointer arithmetic is possible through the unsafe package. Here’s how to do it. Package unsafe … The function Add adds len to ptr and returns the updated pointer unsafe.Pointer(uintptr(ptr) + uintptr(len)) [Go 1.17]. The len argument must be of integer type or an untyped constant. A constant len argument must be representable by a value of type int; if it is an untyped constant it is given type int.

Get daily content like this in your inbox!

Subscribe