Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cast/convert int to rune in Go

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?

like image 202
Roboprog Avatar asked Apr 13 '13 01:04

Roboprog


People also ask

How do I convert an int to a string in Golang?

In order to convert an integer value to string in Golang we can use the FormatInt function from the strconv package. FormatInt returns the string representation of i in the given base, for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z' for digit values >= 10.

How do you cast to int in go?

In order to convert string to integer type in Golang, you can use the following methods. You can use the strconv package's Atoi() function to convert the string into an integer value. Atoi stands for ASCII to integer. The Atoi() function returns two values: the result of the conversion, and the error (if any).

How do you turn a rune into a string?

If you just want to convert a single rune to string , use a simple type conversion. rune is alias for int32 , and converting integer numbers to string : Converting a signed or unsigned integer value to a string type yields a string containing the UTF-8 representation of the integer. There is also utf8.

Can you cast an int to a char C++?

The integer can be converted to a character using the static_cast function. Below is the C++ program to convert int to char using static_cast: C++


2 Answers

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 
like image 107
cthom06 Avatar answered Oct 21 '22 04:10

cthom06


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

like image 22
peterSO Avatar answered Oct 21 '22 04:10

peterSO