Identifier, identifier, variable, constant, iota in GO

Keywords: Go Programming

Reference resources: https://www.cnblogs.com/nickchen121/p/11517455.html

I. identifier and keyword

1. identifier

In programming language, identifier is a special word defined by programmer, such as variable name, constant name, function name, etc. In Go language, identifiers are composed of alphanumeric characters and ﹣ (underscore), and can only begin with letters and ﹣. For example: ABC, uu123, A123

2. keyword

There are 25 keywords in Go language:

o There are 25 keywords in the language:

    break        default      func         interface    select
    case         defer        go           map          struct
    chan         else         goto         package      switch
    const        fallthrough  if           range        type
    continue     for          import       return       var

In addition, there are 37 reserved words in the Go language.

    Constants:    true  false  iota  nil

        Types:    int  int8  int16  int32  int64  
                  uint  uint8  uint16  uint32  uint64  uintptr
                  float32  float64  complex128  complex64
                  bool  byte  rune  string  error

    Functions:   make  len  cap  new  append  copy  close  delete
                 complex  real  imag
                 panic  recover

Two. Variables

1. Variable declaration

//Single variable creation
 var variable name variable type
 //Multiple variable creation
var (
    Variable name variable type
    Variable name variable type
    Variable name variable type
    Variable name variable type
)

2. Variable initialization

var Variable name type = Expression
//Single variable
var name string = "Q1mi"
var age int = 18
//Multiple variables
var name, age = "Q1mi", 20

3. Short variable declaration

Within a function, you can declare and initialize variables in the = method.

package main

import (
    "fmt"
)
// Global variable m
var m = 100

func main() {
    n := 10
    m := 200 // Local variable m is declared here
    fmt.Println(m, n)
}

4. Anonymous variable

If you want to ignore a value when using multiple assignments, you can use anonymous variable. Anonymous variables are indicated by an underscore, for example:

func foo() (int, string) {
    return 10, "Q1mi"
}
func main() {
    x, _ := foo()
    _, y := foo()
    fmt.Println("x=", x)
    fmt.Println("y=", y)
}

Anonymous variables do not occupy the namespace and do not allocate memory, so there is no duplicate declaration between anonymous variables. (in programming languages like Lua, anonymous variables are also called dummy variables. )

matters needing attention:

  1. Every statement outside of a function must start with a keyword (var, const, func, etc.)
  2. : = cannot be used outside a function.
  3. _It is mostly used for occupation, which means the value is ignored.

Three. Constant.

It is very similar to variable declaration, except that var is replaced by const, and constant must be assigned when it is defined.

const pi = 3.1415
const e = 2.7182

After the pi and e constants are declared, their values can no longer be changed during the entire program run.

Multiple constants can also be declared together:

const (
    pi = 3.1415
    e = 2.7182
)

When const declares multiple constants at the same time, if the value is omitted, it means the same value as the above line. For example:

const (
    n1 = 100
    n2
    n3
)

In the above example, the values of constants n1, n2, n3 are all 100.

Four.iota

iota is a constant counter of go language, which can only be used in constant expression.

Iota will be reset to 0 when the const keyword appears. Each new line of constant declaration in const will cause the iota to count once (iota can be understood as the row index in const statement block). Using iota can simplify the definition and is very useful in defining enumerations.

for instance:

const (
        n1 = iota //0
        n2        //1
        n3        //2
        n4        //3
    )

Several common iota examples:

Use to skip some values

const (
        n1 = iota //0
        n2        //1
        _
        n4        //3
    )

iota says cut in the middle

const (
        n1 = iota //0
        n2 = 100  //100
        n3 = iota //2
        n4        //3
    )
    const n5 = iota //0

Define the order of magnitude (here, < means shift left operation, 1 < 10 means shift the binary representation of 1 to the left by 10 bits, that is, from 1 to 100000000000, that is, the decimal 1024). Similarly, 2 < < 2 means to shift the binary representation of 2 to the left by 2 bits, that is, from 10 to 1000, that is, the decimal 8.)

const (
        _  = iota
        KB = 1 &lt;&lt; (10 * iota)
        MB = 1 &lt;&lt; (10 * iota)
        GB = 1 &lt;&lt; (10 * iota)
        TB = 1 &lt;&lt; (10 * iota)
        PB = 1 &lt;&lt; (10 * iota)
    )

Multiple iota defined on one line

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

Posted by Devious Designs on Thu, 24 Oct 2019 04:44:19 -0700