HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to GoVariablesData TypesOperatorsStringsArray & SliceMap (Dictionary)Conditions (if / switch)Loops (for)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
:= (short declaration)varconst
🔢 Numbers & Math
✍️ String
📋 Array & Slice
🗂️ Map
🔁 Condition & Loop
🧩 Function
Go›REFERENCE›Variables
📦

Variables

Variable definitions and constants.

CommandDescription
:= (short declaration)Defines a variable and automatically infers its type.
varVariable definition by explicitly specifying the type.
constImmutable constant.

:= (short declaration)

Try it yourself →

Defines a variable and automatically infers its type.

Syntax

name := value

Description

Most commonly used definition inside functions. No type is given, it is determined by the value.

Example

package main
import "fmt"
func main() {
    name := "Alex"
    age := 36
    fmt.Println(name, age)
}

Expected output

Alex 36

When to use?

Local variable definition.

Common mistakes

  • ⚠️ := is only inside functions; use var at the package level.
  • ⚠️ An unused variable is an ERROR in Go.

var

Try it yourself →

Variable definition by explicitly specifying the type.

Syntax

var name type = value

Description

Used when you want to specify the type or at the package level. If no value is given, it takes the zero value (0, "", false).

Example

package main
import "fmt"
func main() {
    var number int = 42
    var bos string
    fmt.Println(number, bos == "")
}

Expected output

42 true

When to use?

Variables with explicit types or default values.

const

Try it yourself →

Immutable constant.

Syntax

const NAME = value

Description

Constant values known at compile time.

Example

package main
import "fmt"
func main() {
    const Pi = 3.14
    fmt.Println(Pi)
}

Expected output

3.14

When to use?

Constant values.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code