Is there a way to print a string variable in its 2 words format?

Viewed 30

I would like to find a clear way to demonstrate concretely that a variable of type string holds a 2-words data structure (at least as far as I understand it).

This demonstration is for didactic purposes.

So, as I know, a string is a 2 words data structure where one word holds the address of the underlying slice of bytes and the word holds the length.

Given a variable defined like this a := "a string literal", is there a way to view (or print) the content of the variable in its 2 words format so that people can actually see this 2-words structure?

1 Answers

is there a way to view (or print) the content of the variable in its 2 words format?

No, because this is an unspecified implementation detail.

If you are okay with code that might brake: Use reflect.StringHeader. See unsafe.Pointer point (6) on how to do this.

Best not to do this. As said: this is a deliberately hidden implementation detail.

Related