Variables

How to declare variables?

Types

Based on mutability:

  • Immutable (constants) - Cannot be changed

  • Mutable (vars) - Can be changed

Examples

Vars

Without Assignment

// declaration
var name string

// later in the code
name = "Krish Gupta"

With Assignment

var name string = "Krish Gupta"

Inferred

// one
name := "Krish Gupta"

// more than one
name, age := "Krish Gupta", 18

Constants

Explicit Type

const name string = "Krish Gupta"

Inferred Type

const name = "Krish Gupta"

Last updated