Cast/convert int to rune in Go

18,839

Solution 1

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 

Solution 2

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).

Share:
18,839
Roboprog
Author by

Roboprog

I am a programmer, er "Senior Technical Lead" in the Sacramento CA area. I actually like working on computers, when it's not dealing with somebody else's abandoned foul mess. Present and past employer mandated golden hammers: Java; SQL; C; xBase. Other oddments of occasional on the job use or personal interest: Perl; JavaScript; Ruby; C++; (Turbo) Pascal. I have a family at home, and no cats; no blog about cats :-) (we did have dogs, though, but I don't blog about those, either) I make my family run Linux, because I'm evil, and my wife is happy as long as The Google works and keeps working. Maybe next year I'll set up VMWare or something, so every (virtual) Windows start is an automatic fresh reinstall!

Updated on July 03, 2022

Comments

  • Roboprog
    Roboprog almost 2 years

    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?

  • Roboprog
    Roboprog about 11 years
    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.
  • Roboprog
    Roboprog about 11 years
    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!
  • Darshan Rivka Whittle
    Darshan Rivka Whittle about 11 years
    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.)
  • peterSO
    peterSO about 11 years
    Conversions and casts are different. Go does conversions, not casts. Stack Overflow tag descriptions are very far from authoritative.
  • Darshan Rivka Whittle
    Darshan Rivka Whittle about 11 years
    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.
  • Roboprog
    Roboprog about 11 years
    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".
  • fuzzybear3965
    fuzzybear3965 about 5 years
    "PDB"? "Poor Disabled Brogrammer"?