Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Variables & Constants  ›  Lesson

var vs :=

var vs := 12 minvar declares a variable anywhere and can give it just a type, while := declares and initialises in one step, works only inside functions and must introduce at least one new name.

var vs :=

tally needs somewhere to keep things: the file name, the list of words, a running total. Go gives you two ways to declare a variable, and knowing which one fits saves you a surprising number of compile errors.

The rule

var works everywhere and allows a bare type; := is function-only and needs a new name.

var is the full form. You can give a type, a value, or both:

var minLength = 3        // type inferred: int
var total int            // no value: starts at 0
var path string = "notes.txt"

It works at package level (outside any function) and inside functions. A group of them can share one block:

var (
	minLength = 3
	verbose   = false
)

:=, the short declaration, declares and assigns in one step and infers the type from the value:

func main() {
	path := "notes.txt"
	words, err := readWords(path)
	// ... use words and err
}

Its rules:

  1. It only works inside functions. At package level it is a syntax error.
  2. It must declare at least one new variable on the left. total := 0 twice in the same block fails with "no new variables on left side of :=".
  3. With several names, the new ones are declared and the existing ones are simply assigned. That is why a, err := f() followed by b, err := g() is fine: b is new, err is reused.

Plain = never declares; it only assigns to a variable that already exists.

And one rule for both: a local variable you declare but never use is a compile error.

The shortcut

Ask two questions in order: Am I outside a function? Use var. Do I have a starting value? Use :=. If not, var name Type makes the "starts empty" intent obvious.

Declaring tally's variablesStep 1 of 4
  1. Package-level setting

    minLength sits outside every function, so it must be var minLength = 3.

The trap: writing total := 0 again later in the same block to "reset" it. Use total = 0.

Drills

Answer fast — trust the rule.

quick drill

Which line is a compile error at package level, outside any function?

quick drill

Inside one block you write count := 1 and then count := 2. What happens?

quick drill

n, err := parse(a) then, in the same block, m, err := parse(b). Is the second line legal?

quick drill

What is the type of x after x := 7.0?

Remember

  • var works everywhere and allows a bare type; := is function-only and needs a new name.
  • Shortcut: outside a function → var; have a value → :=; want "starts empty" → var name Type.
  • Trap: := twice in one block; plain = is how you change a value.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhich declaration where?

tally needs a package-level setting minLength of 3, a counter total that starts at zero inside main, and the result of words, err := readWords(path). Explain which declaration form you would use for each and why := cannot be used for the first.