Go Reference ยท Concepts
Type conversions in Go
Every Go conversion rule in one page: numeric truncation and overflow, string and rune conversions, strconv, slice to array, named types and why there are no implicit casts.
Go never converts between different types implicitly. To use an int where a float64 is expected, you write the conversion yourself: float64(n). The form T(x) converts the value x to type T when the language allows it. This keeps arithmetic predictable, at the cost of a few extra words.
Conversions fall into groups: between numeric types, between strings and byte or rune slices, between types with the same underlying type, and a few special cases such as slice to array. Parsing text into numbers is not a conversion at all; that is the job of strconv.
var n int = 7 f := float64(n) / 2 // 3.5 s := string(rune(65)) // "A"
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Conversion rules
| From | To | Result |
|---|---|---|
| integer | integer | same value if it fits, else the low bits (wraps) |
| float | integer | fractional part dropped; out-of-range is implementation-defined |
| integer | float | nearest representable value |
| float64 | float32 | rounded; may overflow to infinity |
| integer | string |
the UTF-8 text of that code point (go vet warns unless the source is a rune or byte) |
string |
[]byte / []rune |
a copy of the bytes / the decoded code points |
[]byte / []rune |
string |
a new string; invalid code points become U+FFFD |
| same underlying type | named type | allowed, no change |
| slice | array, or pointer to array | needs length at least the array length, else panic |
| any pointer | unsafe.Pointer |
only through the unsafe package |
Usage notes
- Overflow in conversions is silent. Check ranges yourself when a value comes from input:
if n > math.MaxInt32 { ... }. - Float to int of NaN or a value out of range gives an unspecified value that differs between platforms. Avoid it.
string(i)with anintcompiles but is almost never what you want; usestrconv.Itoa(i)orfmt.Sprint(i).- Converting a
stringto[]byteor back usually allocates and copies. The compiler avoids the copy in some cases (for examplefor range []byte(s), map lookupsm[string(b)], comparisons). - Interfaces use type assertions (
v.(T)) and type switches, not conversions. A conversionI(x)to an interface type is just an assignment that must satisfy the method set. - Untyped constants adapt to context (
var f float64 = 1), sofloat64(1)is rarely needed for literals. math.Round,math.Floor,math.Truncchange float values; converting tointafterwards makes the intent explicit.- Struct types with identical fields (ignoring tags) can be converted to each other.