SoFunction
Updated on 2025-03-03

A brief analysis of the declaration and use of variables and constants in Golang

variable

Types of variables

The function of variables is to store data. The data types stored by different variables may be different. Common data types are generally: integer, floating point, Boolean, etc.

As a strongly typed language, each variable in go has its own type, and the variable must be declared before it can be used.

Declare variables

Variables in go need to be declared before they can be used, and duplicate declarations are not supported in the same scope. And the variables in the go language must be used after declaration.

How to declare variables

Standard Statement

The format of the declared variable is

// var variable name variable type
var name string
var age int
var isShow bool

Batch statement

When declaring variables, you need to write the var keyword every time, it will be more cumbersome. In Go, it also supports batch declaration of variables.

var (
    name string
    age int
    isShow bool
    num float32
)

Initialization of variables

When declaring variables in go, the memory area corresponding to the variable will be automatically initialized, and each variable will be initialized to its default value, for example: the default value of integer and floating-point variables is 0. The default value of string variables is an empty string. The default value of the Boolean variable is false. The default for slices, functions, and pointer variables is nil

But specify the initial value for the variable when declaring it. The standard format for variable initialization is

// var variable name type = valuevar name string = "water"
var age int = 18
// You can also define multiple variables at oncevar name,age = "water", 20

Variable type derivation

Many times, the type of variable can be omitted. The editor can deduce the type of variable based on the value on the right side of the equal sign.

var name = "water"
var age = 18

Short variable declaration

Inside the function, you can use:=Initialize variables in declaration

package main

import (
    "fmt"
)

// Global variablesvar name = "water"

func main() {
    a := 1
    b := 2
    (a,b)
}

Anonymous variables

When using multiple assignments, if you want to ignore a value, you can use anonymous variables. Anonymous variables are generally underlined._Indicates that, as follows

func getValue()(int,string){
    return 18,"water"
}

func main() {
    a,_ := getValue()
    _,b := getValue()
    ("a=",a)
    ("b=",b)
}

Anonymous variables do not occupy namespace and do not allocate memory, so there are no duplicate declarations between anonymous variables. The following issues need to be paid attention to in variable declarations:

  • Each statement outside the function must start with a keyword, such as: var, const, func
  • :=Cannot be used outside the function
  • _Mostly used for placeholding, indicating that the value is ignored

constant

Compared to variables, constants are constant values ​​that define some values ​​that will not change. The declaration of a constant is very similar to the declaration of a variable, but var is replaced with const. Const must be assigned a value when defining it.

const a = 123
const b = 456

Constants will not change again once declared. And multiple constants can be declared together

const (
    a = 123
    b = 456
)

If multiple constants are declared at the same time, the value is omitted, which means the value is the same as the above line.

const (
    a = 1
    b
    c
)

In this way, the values ​​of a, b, and c are all 100

iota

iotaIt is a constant counter in Go language and can only be used in constant expressions.iotaIt is 0 when it first appears in the const keyword, and then every new row of constant declarations are added to the const, the count will be added.

const (
    a = iota // 0
    b // 1
    c // 2
    d // 3
)

Common tips for using iota

use_Skip some values

const (
    a = iota //0
    b // 1
    _
    d // 3
)

Insert other values ​​in the middle of iota

const (
    a = iota // 0
    b = 20
    c // 2
    d // 3
)

const e = iota // 0

Multiple iota definitions one line

const (
    a,b = iota + 1,iota + 2 // 1,2
    c,d // 2,3
    e,f // 3,4
)

This is the article about briefly analyzing the declaration and use of variables and constants in Golang. For more related Golang variables, please search for my previous articles or continue browsing the related articles below. I hope you will support me in the future!