Variables
A variable is a named slot that holds a value. Go gives you two ways to declare one, and knowing when to use which is half the lesson.
var: the full form
package main
import "fmt"
var greeting string = "hello"
func main() {
fmt.Println(greeting)
}
var name type = value spells everything out. But the compiler can infer
the type from the value, so var greeting = "hello" works too — "hello" is
a string, so greeting is a string. var works everywhere: at package level
(like above) or inside functions.
:=: the short form
Inside a function, Go offers a shortcut that declares and initializes in one stroke:
package main
import "fmt"
func main() {
greeting := "hello"
count := 3
fmt.Println(greeting, count)
}
:= means "declare a new variable and give it this value, inferring the
type". It's what you'll write most of the time — but it only works inside
functions. At package level, every statement must begin with a keyword
(var, func, const, ...), so greeting := "hello" outside a function is
a compile error. Rule of thumb: := inside functions, var outside.
Zero values
What if you declare a variable but don't initialize it?
package main
import "fmt"
func main() {
var s string
var i int
var f float64
var b bool
fmt.Println(s, i, f, b) // " 0 0 false" — s is the empty string
}
In many languages this is where "uninitialized memory" bugs live. Go refuses
to have them: every type has a zero value — "" for strings, 0 for
numbers, false for booleans — and a declared-but-unassigned variable holds
it, guaranteed. There is no such thing as an undefined variable value in Go.
Unused variables are compile errors
Try this and the compiler stops you:
package main
func main() {
unused := 42 // compile error: declared and not used
}
Not a warning — an error. Why so harsh? An unused variable is usually a bug in disguise: a typo'd name, a half-finished refactor, a value you meant to use. Warnings get ignored and pile up; Go's designers decided the code simply shouldn't compile. Your files stay honest, the same way unused imports do.
Multiple assignment
Go can assign several variables in one statement, left-to-right names matching left-to-right values:
package main
import "fmt"
func main() {
a, b := "first", "second"
a, b = b, a // swap! the right side is evaluated before any assignment
fmt.Println(a, b)
}
That swap line is the famous party trick: because Go evaluates the entire
right-hand side before assigning, a, b = b, a exchanges two values with
no temporary variable. (Note it's = the second time, not := — the
variables already exist, we're only assigning.)
Multiple assignment is also how you receive a function's multiple return values, which is exactly what the exercise asks you to produce.
Practice
1. Swap two values
BeginnerImplement the function
swapwith exactly this signature:func swap(a, b string) (string, string)It must return its two arguments in the opposite order:
swap("left", "right")returns"right", "left".You can do it with multiple assignment, or with a single
return— both are idiomatic Go.package main import "fmt" // swap returns its two arguments in the opposite order. func swap(a, b string) (string, string) { // TODO: implement — return b first, then a. return "", "" } func main() { first, second := swap("left", "right") fmt.Println(first, second) }Sign in to write and run your solution in the browser.
Sign in for hints and the solution.