3. Some tips on Go language

grouping statement

In the Go language, when declaring multiple constants and variables at the same time, or importing multiple packages, you can declare them in groups.
For example the following code:

import "fmt"
import "os"

const i = 100
const pi = 3.1415
const prefix = "Go_"

var i int
var pi float32
var prefix string

It can be written in groups as follows:

import(
    "fmt"
    "os"
)

const(
    i = 100
    pi = 3.1415
    prefix = "Go_"
)

var(
    i int
    pi float32
    prefix string
)

Unless explicitly set to another value or iota, the first constant of each const group is defaulted to its value of 0, and the second and subsequent constants are defaulted to the value of the preceding constant. If the preceding constant The value is iota , then it is also set to iota .

iota enumeration

There is a keyword iota in Go. This keyword is used when declaring enum. Its default starting value is 0, and it increases by 1 each time it is called:

const(
    x = iota // x == 0
    y = iota // y == 1
    z = iota // z == 2
    w // 常量声明省略值时,默认和之前一个值的字面相同。这里隐式地说 w = iota ,因此 w == 3 。其实上面 y 和 z 可同样不用 "= iota"
)
const v = iota // 每遇到一个 const 关键字, iota 就会重置,此时 v == 0

Some rules of Go programming

The reason why Go is so simple is that it has some default behaviors:

  • Variables starting with an uppercase letter are exportable, that is, can be read by other packages, and are public variables; variables starting with a lowercase letter are not exportable, and are private variables.
  • The same goes for functions starting with capital letters, which are equivalent to public functions with the public keyword in the class; functions starting with lowercase letters are private functions with the private keyword.

Guess you like

Origin blog.csdn.net/u012534326/article/details/119986197