Values and Types
Programs compute with values, and in Go every value has a type the compiler knows about before the program ever runs. Let's meet the four you'll use constantly:
package main
import "fmt"
func main() {
fmt.Println("gopher", 42, 3.14, true)
}
That's a string, an int, a float64, and a bool — and one useful trick:
fmt.Println accepts any number of values and prints them separated by
spaces. Run it and you get gopher 42 3.14 true.
Static typing
Go is statically typed: the type of every value is fixed at compile time.
42 is an integer, 3.14 is a floating-point number, and the compiler will
never quietly treat one as the other. This sounds strict, but it's a trade
Go makes deliberately — a whole class of bugs ("I thought this was a number,
it was text") gets caught before the program runs, not in production at 3am.
The flip side: Go does no implicit conversions. Even int and float64
don't mix:
package main
import "fmt"
func main() {
votes := 42
turnout := 3.14
fmt.Println(float64(votes) * turnout) // convert explicitly, or it won't compile
}
Writing votes * turnout directly is a compile error: invalid operation: mismatched types int and float64. If you want a conversion, you say so —
float64(votes) — and every reader can see exactly where it happens.
Strings and +
The + operator is doing double duty in Go. Between numbers it adds; between
strings it concatenates:
package main
import "fmt"
func main() {
fmt.Println("go" + "pher") // gopher
fmt.Println(1 + 2) // 3
}
But it never crosses the streams: "gopher" + 42 is a compile error, not
"gopher42". Languages that guess for you here (is it addition? gluing?
something else?) produce famous surprises; Go simply refuses. If you need a
number inside a string, fmt will format it for you — that's its job.
Integer division vs float division
Here's the classic beginner trap, in one program:
package main
import "fmt"
func main() {
fmt.Println(7 / 2) // 3
fmt.Println(7.0 / 2) // 3.5
}
Dividing two integers is integer division: the result is an integer and the
remainder is thrown away, so 7 / 2 is 3, not 3.5. Make either operand a
float — 7.0 / 2 — and you get float division, 3.5. When a division looks
wrong by roughly half, check the types of the operands first; it's almost
always this.
Functions can return several values
One more Go signature move you'll need for the exercise: a function can return more than one value, and the return types are listed in parentheses:
func stats() (string, int) {
return "answers", 42
}
The return supplies one value per listed type, in order. The exercise below
asks you to return one value of each type from this lesson.
Practice
1. Type report
BeginnerImplement the function
describewith exactly this signature:func describe() (string, int, float64, bool)It must return, in order:
- the string
"gopher" - the integer
42 - the float
3.14 - the boolean
true
One value of each of the four basic types from the lesson — returned in a single
returnstatement.package main import "fmt" // describe returns one value of each basic type: a string, an int, a // float64, and a bool. func describe() (string, int, float64, bool) { // TODO: implement — return "gopher", 42, 3.14 and true, in that order. return "", 0, 0, false } func main() { s, i, f, b := describe() fmt.Println(s, i, f, b) }Sign in to write and run your solution in the browser.
Sign in for hints and the solution.
- the string