Summary - Variables
- Go course
- Beginner
- Lesson 18/90
- 4 min
Combine var, :=, const and zero value to write a profile.
Summary - Variables
In this section you learned variable declaration:
- Explicit declaration and type annotation with var
- Short declaration with := (function scope only)
- Constants with const and auto-increment with iota
- Every type has a zero value (0 for int, empty string for string)
- Go infers the type from the right-hand side
Now you will combine these concepts to write a profile card. The goal: pick the right declaration form and start safely from the zero value.
Practice Constant and variable
Define the constant const KDV = 0.20 and the variable fiyat := 150.0. Compute the price with VAT using toplam := fiyat * (1 + KDV) and print it.
Starter code
package main
import "fmt"
func main() {
// buraya yaz
}Sample solution
package main
import "fmt"
const KDV = 0.20
func main() {
fiyat := 150.0
toplam := fiyat * (1 + KDV)
fmt.Println(toplam)
}Expected output
180
Practice Multiple assignment
Declare two variables with a, b := 3, 7. Swap them in one line (a, b = b, a) and print fmt.Println(a, b).
Starter code
package main
import "fmt"
func main() {
// buraya yaz
}Sample solution
package main
import "fmt"
func main() {
a, b := 3, 7
a, b = b, a
fmt.Println(a, b)
}Expected output
7 3
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Short declaration operator
To assign 5 to x with the short form:
x ____ 5
To assign 5 to x with the short form:
x := 5
Explanation: := is the short declaration operator.
2 Match the section concepts
| Term | Meaning |
|---|---|
var |
Explicit decl |
:= |
Short decl |
const |
Constant |
iota |
Auto-increment |