Cast/convert int to rune in Go
Asked Answered
S

2

11

Assuming I have an int64 variable (or other integer size) representing a valid unicode code-point, and I want to convert it into a rune in Go, what do I do?

In C I would have used a type cast something like:

c = (char) i;  // 7 bit ascii only

But in Go, a type assertion won't work:

c, err = rune.( i)

Suggestions?

Saum answered 13/4, 2013 at 1:42 Comment(1)
int32(i) or rune(i) to set an int to a rune. See https://mcmap.net/q/55267/-idiomatic-type-conversion-in-go. int(r) to set a rune to an int. See https://mcmap.net/q/56310/-golang-how-does-the-rune-function-work.Diuresis
V
21

You just want rune(i). Casting is done via type(x).

Type assertions are something different. You use a type assertion when you need to go from a less specific type (like interface{}) to a more specific one. Additionally, a cast is checked at compile time, where type assertions happen at runtime.

Here's how you use a type assertion:

var (
    x interface{}
    y int
    z string
)
x = 3

// x is now essentially boxed. Its type is interface{}, but it contains an int.
// This is somewhat analogous to the Object type in other languages
// (though not exactly).

y = x.(int)    // succeeds
z = x.(string) // compiles, but fails at runtime 
Vivienne answered 13/4, 2013 at 1:44 Comment(2)
D'oh! That works great: use type-name in what looks like a function call to cast. Somehow, in piling through the tutorial, I found quite a bit about type assertions, but little about casting.Saum
The funny thing, I've used type assertions in a way like java's instanceof, but like I said, had not run across a simple cast yet. Thanks for the help!Saum
P
3

In Go, you want to do a conversion.

Conversions

Conversions are expressions 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 ")" .

A non-constant value x can be converted to type T in any of these cases:

  • x is assignable to T.
  • x's type and T have identical underlying types.
  • x's type and T are unnamed pointer types and their pointer base types have identical underlying types.
  • x's type and T are both integer or floating point types.
  • x's type and T are both complex types.
  • x is an integer or has type []byte or []rune and T is a string type.
  • x is a string and T is []byte or []rune.

You want to convert x, of type int, int32, or int64, to T of type rune, an alias for type int32. x's type and T are both integer types.

Therefore, T(x) is allowed and is written rune(x), for your example, c = rune(i).

Polypoid answered 13/4, 2013 at 4:8 Comment(5)
The casting tag defines casting as "a process where an object type is explicitly converted into another type if the conversion is allowed." Would "casting" not be the language-agnostic word for what Go calls "conversions"? I think there's value in leaving the tag on the question, unless there's a stronger difference in meaning than I realize. (In which case I'd love clarification.)Loveliesbleeding
Conversions and casts are different. Go does conversions, not casts. Stack Overflow tag descriptions are very far from authoritative.Polypoid
Googling around, I'm still having a very hard time discovering if there's a difference. I find many things such as "Casting represents a request by the programmer to do an explicit type conversion." That's how I think of casting, and I think that's how the typical programmer thinks of it. Please clarify the important distinction, or let's add the tag back to make it easier for people to find this question (and your helpful answer) when (quite understandably, as far as I'm concerned) searching for "[go] [casting]" or similar.Loveliesbleeding
Thanks, Darshan. I got the wrong term, cuz I was tired, and couldn't remember the name of the thing I really wanted (if I had, there probably would have been no question in the first place, as it's so obvious). That said, "cast" is probably the term that the "PDB" trying to do this is going to use when struggling with doing a type conversion the first time -- it aids search, even if it's "wrong".Saum
"PDB"? "Poor Disabled Brogrammer"?Catalpa

© 2022 - 2024 — McMap. All rights reserved.