Identifiers and keywords

identifier

In programming languages, identifiers are words defined by programmers with special meaning, such as variable names, constant names, function names, and so on. In Go, identifiers consist of alphanumeric characters and underscores (_) and can only start with a letter or underscore. A few examples: ABC, _, _123, a123.

The keyword

Keywords are predefined identifiers with special meanings in a programming language. Neither keyword nor reserved word is recommended for variable names. There are 25 keywords in Go:

  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
Copy the code

In addition, there are 37 reserved words in Go.

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
Copy the code

variable

Origin of variable

Program is running in the process of data is stored in memory, when we want to operate some data in the code you need to find the variables on the memory, but if we directly in the code through memory address to operating variables, the readability of the code will be very bad but also easy to get wrong, so we can use variables to save the data memory address, We can use this variable directly to find the corresponding data in memory

Variable types

The function of variables is to store data. Different variables may hold different data types. After more than half a century of development, programming languages have basically formed a set of fixed types, common variable data types are: integer, floating point, Boolean and so on.

Each variable in Go has its own type, and variables must be declared before they can be used.

Variable declarations

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

Standard statement

The variable declaration format of Go language is:

Var Variable name Variable typeCopy the code

Variable declarations start with the keyword var, place the variable type after the variable, and do not require a semicolon at the end of the line. Here’s an example:

var name string
var age int
var isOk bool
Copy the code

Batch statements

It is cumbersome to write the var keyword for each variable declaration. Go also supports batch variable declarations:

var (
    a string
    b int
    c bool
    d float32
)
Copy the code

Initialization of a variable

When declaring a variable, Go automatically initializes the memory region corresponding to the variable. Each variable is initialized to a default value of its type, such as 0 for integer and floating point variables. The default value of a string variable is an empty string. Boolean variables default to false. Defaults to nil for slices, functions, and pointer variables. We can also specify an initial value for a variable when we declare it. The standard format for variable initialization is as follows:

Var Variable name type = expressionCopy the code

Here’s an example:

var name string = "Q1mi"
var age int = 18
Copy the code

Or initialize multiple variables at once

var name, age = "Q1mi", 20
Copy the code

Type inference

Sometimes we omit the type of the variable, and the compiler initializes the variable from the value to the right of the equals sign.

var name = "Q1mi"
var age = 18
Copy the code

Short variable declaration

Inside a function, variables can be declared and initialized using the simpler := method.

Package main import (" FMT ") m var m = 100 func main() {n := 10 m := 200Copy the code

Anonymous variable

When using multiple assignments, you can use anonymous variables if you want to ignore a value. Anonymous variables are represented 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)
}
Copy the code

Anonymous variables take up no namespace and no memory is allocated, so there are no duplicate declarations between anonymous variables. (In programming languages like Lua, anonymous variables are also called dumb variables.) Matters needing attention:

  1. Every statement outside a function must start with a keyword (var, const, func, etc.)
  2. : =Cannot be used outside a function.
  3. _Usually used as a placeholder to indicate that a value is ignored.

constant

Constants are constant values, as opposed to variables, and are used to define values that do not change during program execution. Constants are declared in much the same way as variable declarations, except that var is replaced by const, and constants must be assigned when defined.

Const PI = 3.1415 const e = 2.7182Copy the code

Once PI and e are declared, their values cannot change for the duration of the program. Multiple constants can also be declared together:

Const (PI = 3.1415 e = 2.7182)Copy the code

Const declares multiple constants at the same time, if the value is omitted, it is the same as the value in the previous line. Such as:

const (
    n1 = 100
    n2
    n3
)
Copy the code

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

iota

Iota is a constant counter for the GO language and can only be used in constant expressions.

Iota is reset to 0 when the const keyword is present. Iota counts once for each new line of constant declaration in const. Using IOTA simplifies definitions and is useful when defining enums.

Here’s an example:

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

A few common onesiotaExample:

Skip some values with _

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

Iota claims to cut in line

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

Define the order of magnitude (where << represents a left-shift operation, and 1<<10 represents a 10-bit shift to the left of the binary representation of 1, from 1 to 10000000000, or 1024 decimal. Similarly, 2<<2 shifts the binary representation of 2 two bits to the left, from 10 to 1000, which is the decimal 8.)

const ( _ = iota KB = 1 << (10 * iota) MB = 1 << (10 * iota) GB = 1 << (10 * iota) TB = 1 << (10 * iota) PB = 1 << (10 *  iota) )Copy the code

Multiple IOtas are defined on a single line

Const (a, b = iota + 1, iota + 2 //1,2 c, d //2,3 e, f //3,4)Copy the code