Conversions

February 26, 2024

On todays’s livestream, we’ll be Pair Programming on a real project. I hope to see you there!


Conversions

A conversion changes the type of an expression to the type specified by the conversion. A conversion may appear literally in the source, or it may be implied by the context in which an expression appears.

An explicit conversion is an expression of the form T(x) where T is a type and x is an expression that can be converted to type T.

Conversion = Type "(" Expression [ "," ] ")" .

This seems like a good time to address a common confusion of terminology among beginning Go programmers:

As we see above, Go supports type conversion. But you may see this incorrectly referred to as type casting. What’s the difference? Why does it matter?

While there’s some ambiguity around these terms (as there are with most English language terms), generally speaking, type casting presents the same underlying memory through a different type.

As a simple example, given the in-memory bytes 1001000 and 1101001, this could represent the string Hi, the 16-bit integer 18537, or two 8-bit integer values, 72 and 105. And in a language like C, you can change the interpretation of the same memory locations through type casting, without allocating any new memory.

Except through clever use of the unsafe package, Go does not support this sense of type casting. Rather, it does type conversion.

So that’s the difference. (Why) does it matter?

Well, it often doesn’t matter if someone uses the “wrong” term to describe converting from a 16-bit integer to a 32-bit integer, for example. What does matter is a recognition that when you convert types in Go, you allocate new memory. If you’ve come from a language like C, where type casting uses existing memory, this can potentially have a big impact on the performance and memory usage of your program.

On the other hand, if you didn’t know the difference between type casting and type conversion before reading this, or if you’re still confused, then it probably does not matter. At least not in a practical sense.

It still might be nice to use the “technically correct” term, though. You decide. 😊

Quotes from The Go Programming Language Specification Language version go1.22 (Feb 6, 2024)


Share this

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

Unsure? Browse the archive .

Related Content


Case expressions

I’m back live streaming again! Join me in just over an hour! Bring your questions, too! Expression switches … If a case expression is untyped, it is first implicitly converted to the type of the switch expression. For each (possibly converted) case expression x and the value t of the switch expression, x == t must be a valid comparison. In other words, the switch expression is treated as if it were used to declare and initialize a temporary variable t without explicit type; it is that value of t against which each case expression x is tested for equality.


Conversions from slice to array or array pointer

Conversions from slice to array or array pointer … Converting a slice to an array yields an array containing the elements of the underlying array of the slice. Similarly, converting a slice to an array pointer yields a pointer to the underlying array of the slice. In both cases, if the length of the slice is less than the length of the array, a run-time panic occurs. s := make([]byte, 2, 4) a0 := [0]byte(s) a1 := [1]byte(s[1:]) // a1[0] == s[1] a2 := [2]byte(s) // a2[0] == s[0] a4 := [4]byte(s) // panics: len([4]byte) > len(s) s0 := (*[0]byte)(s) // s0 !


Converting integers to strings

Sorry I missed yesterday. The family road trip ended later than expected, and I was too shot to write anything. Conversions to and from a string type … Finally, for historical reasons, an integer value may be converted to a string type. This form of conversion yields a string containing the (possibly multi-byte) UTF-8 representation of the Unicode code point with the given integer value. Values outside the range of valid Unicode code points are converted to "\uFFFD".

Get daily content like this in your inbox!

Subscribe