3

I only started Go today, so this may be obvious but I couldn't find anything on it.

What does var x uint64 = 0x12345678; y := string(x) give y?

I know var x uint8 = 65; y := string(x) would give y the byte 65, character A, and common sense would suggest (since types larger than uint8 are allowed to be cast to strings) that they would simply be packed in to native byte order (i.e little endian) and assigned to the variable.

This does not seem to be the case:

hex.EncodeToString([]byte(y)) ==> "efbfbd"

First thought says this is an address with the last byte being left off because of some weird null terminator thingy, but if I allocate two x and y variables with two different values and print them out I get the same result.

var x, x2 uint64 = 0x10000000, 0x20000000
y, y2 := string(x), string(x2)
fmt.Println(hex.EncodeToString([]byte(y))) // "efbfbd"
fmt.Println(hex.EncodeToString([]byte(y2))) // "efbfbd"

Maddeningly I can't find the implementation for the string type anywhere although I probably haven't looked hard enough.

1 Answer 1

5

This is covered in the Spec: Conversions: Conversions to and from a string type:

Converting a signed or unsigned integer value to a string type yields a string containing the UTF-8 representation of the integer. Values outside the range of valid Unicode code points are converted to "\uFFFD".

So effectively when you convert a numeric value to string, it can only yield a string having one rune (character). And since Go stores strings as the UTF-8 encoded byte sequences in memory, that is what you will see if you convert your string to []byte:

Converting a value of a string type to a slice of bytes type yields a slice whose successive elements are the bytes of the string.

When you try to conver the 0x12345678, 0x10000000 and 0x20000000 values to string, since they are outside of the range of valid Unicode code points, as per spec they are converted to "\uFFFD" which in UTF-8 encoding is []byte{239, 191, 189}; when encoded to hex string:

fmt.Println(hex.EncodeToString([]byte("\uFFFD"))) // Output: efbfbd

Or simply:

fmt.Printf("%x", "\uFFFD") // Output: efbfbd

Read the blog post Strings, bytes, runes and characters in Go for more details about string internals.

And btw since Go 1.5 the Go runtime is implemented (mostly) in Go, so these conversions are now implemented in Go and can be found in the runtime package: runtime/string.go, look for the intstring() function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.