← Back to articles

Learning Go (3): variables and constants

Part 3 of my Go fundamentals notes: declarations, constants, iota, and scope.

Variables and constants

Go is statically typed: variable types are determined at compile time.

Declare variables before using them.

Every variable has a type.

A variable’s type does not change after declaration.

1. Variables

1.1. Ways to declare a variable

// 第一种
var age int (string bool 等)
age = 10
// 第二种
var age = 10
// 第三种
age := 10

1.2. Package-level and local variables

Package-level variables are declared outside functions and can be used within the package.

Local variables are declared inside functions and follow the scope of their enclosing block.

Unused package-level variables are allowed, but unused local variables cause a compilation error.

1.3. Declaring multiple variables

// 第一种
var user1, user2, user3 string
// 第二种
var user1, user2, user3 = "user1", 1, true
// 第三种
var (
	user1 string
	user2 int
	user3 bool
)

1.4. Things to remember

  1. Declare a variable before use.
  2. Assignments must be compatible with the variable’s type.
  3. Names must not conflict in the same declaration scope.
  4. Short declarations, such as age := 1, cannot be used at package level.
  5. Variables have default zero values.
var age int //age默认为0
var name string //name默认为""
var isOk bool //isOk默认为false
  1. Unused package-level variables are permitted; unused local variables produce an error.

2. Constants

A constant’s value is specified in its declaration and cannot be changed.

The examples use uppercase constant names to distinguish them; this is a style choice rather than a language requirement.

2.1. Ways to declare a constant

// 第一种
const PI float32 = 3.1415926 //显示定义
const PI = 3.1415926 //隐式定义
// 之后PI不能改变
PI = 3.1415926 //报错

2.2. Grouped declarations

Several forms are available for grouped declarations.

const(
	PI float32 = 3.1415926
	PI2 = 'a'
	PI3 = 3
)

A special case:

cosnt(
    x int  = 1p
    y
    s = 'a'
    z
    m
)

go

2.3. Things to remember

  1. Constants can be boolean, numeric, or string values.
  2. Constants need not be used after declaration.
  3. An explicitly typed constant must be representable by its specified type.

3、iota

A special identifier for generating successive constant values.

3.1. Using iota

//写法1
const (
    ERR0 = iota
    ERR1 = iota
    ERR2 = iota
    ERR3 = iota
    ERR4 = iota
)
// 输出为:0、1、2、3、4
//写法2
const (
    ERR0 = iota
    ERR1
    ERR2
    ERR3
    ERR4
)
// 输出也为0、1、2、3、4

The counter behavior of iota is shown here: go

go

The counter advances for successive constant specifications, regardless of the values assigned.
A new const declaration starts iota at zero again.

3.2. Things to remember

  1. A changed expression may need to explicitly reintroduce iota.
  2. iota represents successive untyped integer constants.
  3. It simplifies groups of related constants.
  4. Each new const declaration resets the count to zero.

4. The blank identifier

Use an underscore for a value that will be discarded; no unused-variable error is produced.

For example: var _ int = 10

The blank identifier is a placeholder, often used to discard some values from a function with multiple return values. Here is an example: go

5. Variable scope

5.1. Package-level variables

Variables declared outside functions are available within the package, subject to Go’s visibility rules.

5.2. Local variables

Variables declared inside a function are limited by their enclosing block.

Pay attention to these boundaries when using a variable.

About this article’s date

The date recorded on the old site is retained; writing may have started earlier.

In this series · Go fundamentals

PreviousLearning Go (2): program structure and execution ←NextLearning Go (4): basic types →