0Pricing
Go Academy · Lesson

Declaring Variables in Go

var, :=, and type inference

Declaring Variables in Go is a free Go Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is a Variable?

A variable is a named storage location for a value. In Go, every variable has a type that is fixed at compile time.

  • Go is statically typed — types cannot change at runtime
  • Variables always have a default zero value when not explicitly set
  • Clear naming matters: Go favors short, clear names over long ones

var Declaration

The var keyword declares one or more variables explicitly:

package main

func main() {
    var name string
    var age  int
    var pi   float64 = 3.14
    _ = name
    _ = age
    _ = pi
}

Short Variable Declaration :=

Inside functions you can use := for a concise declaration + assignment in one step.

  • Type is inferred from the right-hand side
  • := is only available inside a function body
  • At least one variable on the left must be new
package main

func main() {
    city := "Ankara"   // string inferred
    count := 42         // int inferred
    temp := 36.6        // float64 inferred
    _ = city
    _ = count
    _ = temp
}

Zero Values

Go initializes every declared variable to its zero value automatically:

  • int, float640
  • string"" (empty string)
  • boolfalse
  • Pointers, slices, maps, channels → nil

Zero values eliminate uninitialized-variable bugs common in C/C++.

Multiple Variable Declaration

Declare and assign several variables at once using a block or tuple form:

package main

var (
    host = "localhost"
    port = 8080
    debug = false
)

func main() {
    x, y := 10, 20
    _ = x
    _ = y
}

Type Inference Rules

When you use :=, Go infers the type from the literal:

  • Integer literals → int
  • Floating-point literals → float64
  • String literals → string
  • Complex literals → complex128

If you need a specific type (e.g., int32), use an explicit var declaration or a conversion.

Package-Level vs Function-Level Variables

Variables can live at package level (global to the package) or function level (local to the function):

  • Package-level: use var — accessible from anywhere in the package
  • Function-level: use := or var — only visible inside the function
  • Prefer function-level variables to keep state contained

Blank Identifier _

The blank identifier _ discards a value you don't need. Go requires every declared variable to be used — _ is the escape hatch:

package main

func divide(a, b int) (int, int) {
    return a / b, a % b
}

func main() {
    quotient, _ := divide(10, 3) // discard remainder
    _ = quotient
}

Shadowing Variables

Shadowing happens when an inner scope declares a variable with the same name as an outer scope:

  • The inner variable hides the outer one within its scope
  • This can cause subtle bugs — be intentional about it
  • Use go vet and linters like staticcheck to detect accidental shadowing

Common Mistakes with :=

Watch out for these := pitfalls:

  • Using := at package level → compile error
  • All variables on the left already exist → use = instead
  • Accidentally shadowing an outer error variable in if-err blocks

When in doubt, use explicit var for clarity.

Quick Check

Which of the following is the correct short variable declaration syntax in Go?

Recap: Declaring Variables

You now know how to declare variables in Go:

  • var name type — explicit declaration with zero value
  • var name = value — var with type inference
  • name := value — short declaration inside functions
  • Zero values are automatic — no uninitialized state
  • _ discards unwanted values

Next: Go's built-in types and their zero values in detail.

Practice Prompt

Try it yourself: declare a package-level variable appName of type string and a function-level variable retries using := set to 3.

Remember: package-level variables use var, and := is only valid inside function bodies.

Frequently asked questions

Is the “Declaring Variables in Go” lesson free?

Yes — the full text of “Declaring Variables in Go” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

What will I learn in “Declaring Variables in Go”?

var, :=, and type inference You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Declaring Variables in Go” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Declaring Variables in Go
  2. Go's Built-in Types
  3. Constants and iota
  4. Type Conversions in Go
← Back to Go Academy