Type conversions

Go does not convert between types automatically. If you have an int and a function expects a float64, the compiler will stop you until you spell out the conversion:

var a int = 3
var b float64 = 1.5
c := a + b   // compile error: mismatched types int and float64

This is deliberate. Languages that convert silently (C promoting int to double, JavaScript coercing numbers to strings) introduce a quiet category of bugs, and Go chose to reject that trade. To make the program above compile, you convert one side to match the other.

The conversion syntax

The conversion syntax looks like a function call: you name the target type, with the value to convert in parentheses.

var a int = 3
var b float64 = 1.5
c := float64(a) + b   // now both operands are float64, c is 4.5

float64(a) produces a new float64 value equal to 3. The variable a is unchanged, since Go works with values, not references, for numeric types. Any compatible pair works the same way:

i := 42
f := float64(i)   // int to float64
back := int(f)    // float64 to int
short := int8(i)  // int to int8 (may wrap if out of range)

Float-to-int truncates, does not round

When you convert a float64 to an int, Go drops the fractional part. It does not round:

var x float64 = 3.1
var y float64 = 3.9
var z float64 = -3.9

fmt.Println(int(x))   // 3
fmt.Println(int(y))   // 3
fmt.Println(int(z))   // -3   (toward zero, not minus-infinity)

There is no "round to nearest" option built into the conversion. When you need proper rounding, use math.Round and convert the result:

import "math"

x := 3.7
rounded := int(math.Round(x))   // 4

math.Round rounds half away from zero, handles negatives correctly, and works at the boundaries where the naive int(x + 0.5) trick silently breaks. math.Floor and math.Ceil are there when you want explicit down or up rounding. The math package is covered in the Standard library chapter.

The string(65) trap

Converting a number to a string with string(n) does NOT give you the decimal text of the number:

s := string(65)   // "A", not "65"

Go treats the integer as a Unicode code point and returns the single-character string for that code point. 65 is the code point for the capital letter A, so string(65) gives you "A". This is useful when you already have a code point and want a one-character string, but it is almost never what someone coming from another language expects.

To get the decimal text of a number, reach for strconv.Itoa:

import "strconv"

s := strconv.Itoa(65)   // "65"

The strconv package handles text-and-number conversions in both directions (Atoi parses a string into an int) and is covered in detail in the Standard library chapter.

Gotcha

string(65) returns "A", not "65". Try it: add fmt.Println(string(65)) to main and run. You will see the single letter A. If you wanted the text "65", strconv.Itoa(65) is the answer.

Task

The starter computes 10 / 3 as a float. Add a second fmt.Println that does the same computation with integer division instead:

fmt.Println(whole / 3)

Run it. You will see the difference: float division gives 3.333... while integer division truncates to 3. This happens because when both operands are integers, Go performs integer division, which discards the remainder and keeps only the whole number. The fractional part has nowhere to go in an int, so Go drops it (the same truncation-toward-zero rule from the float-to-int section above).

Expected output
3.3333333333333335
3