Golang basis of variables (Variable) of those pits

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/AMimiDou_212/article/details/100103494

First, declare variables and

1.1 flag (the Identify)

标记
标识符-identifiers
字母 - 数字 - 下划线
关键字-keywords
25 个
运算符-operators
字面量-literals
真实值
标点符号-punctuation
逗号

Specification defines:

  1. Identifier named program entities.
  2. Identifier of one or more Unicode sequence of letters and numbers. But the first character of the identifier must be a Unicode letters ( letters and underscore _ )
  3. Short variable declarations (short form statement short variable declarations), will automatically infer the type to go out with in accordance with its environment. In short variable declarations inside the function use.
  4. Note Jane declare global variables and occasional hidden variables generated that global variables are overwritten. example:
var x = 10 //全局变量,作用域整个包块
var b = 15

func Variable() {
	//var y = 56   // y declared and not used
	x := 15 //局部变量  作用域 仅限当前代码块,当前x 覆盖全局变量x
	fmt.Println(x)  //15
	
	{
		fmt.Println(x)  // 15
		x := 12 //局部变量 ,作用域当前
		fmt.Println(x)  //12
	}
	fmt.Println(x)  //15

}
  1. Go mandatory language, in the function declaration must use all of the variables , variables if there are unused, it will compile errors (see below) occurs, and therefore does not require a variable may be used directly or using a blank identifier _ Comment out. But the use of global variables is not restricted, as in the above global variables b, declared but not used, no compilation errors occurred.
y declared and not used
  1. _ (Blank identifier) ​​is actually a write-only variable, you can not get his value.
  2. GO language, if not using the imported packages, also can not be compiled. If you do not use direct function of the bag, and just call package init () function, or remove the debug code using the function of some packages, you can use an underscore identifier, as the package name, to avoid compilation failure .
  3. Variable is referenced generally stored in the heap memory, so that garbage collection system () GC).

Second, the zero value (nil)

  1. When a variable is declared var, its initial value if no declaration, Go automatically initializes its default value zero value for that type.
  2. Zero value nil, as follows:
nil
pointer
interface
function
map
channel
slice
  1. Zero value of the composite type, Go recursion will automatically initialize each element corresponding to the type of its zero value.
  2. Go language, requires special attention, a value of zero string string is not nil, but "";
var  str string
fmt.Println(str)  // "" 
  1. Adding elements to a nil slice is no problem, but do need to pay attention to a map when panic panic run the same thing will happen. 1
func Nil() {
	// dacalaration nil slice
	var slice []int = nil

	// append element
	slice = append(slice, 1, 2)
	// append(slice, 1, 2)
	fmt.Println(slice)
}

Run-time panic occurs nil map.

func MapNil(){
	var m map[string]int

	m["a"] = 1   //panic: assignment to entry in nil map
	fmt.Println(m["a"])   
}

Three, Golang special operators

1. Press the zero position & ^

We will refer to the value set on the zero positioning. (The operator left data distinct bits are reserved, the same zero position )

func Options() {
	X := 2 //0000 0010
	Y := 4 //0000 0100

	c := X &^ Y
	d := X & (^Y)

	fmt.Printf("%.8b %d\n", c, c)   //00000010 2
	fmt.Printf("%.8b %d\n", d, d)  //00000010 2
	fmt.Printf("%+v\n", c == d) //X &^ Y==  X & (^Y)  //true
}
calculation process:

X, Y, respectively, in binary, & ^ 0000 0010 0000 0100 0000 0010 =. If y is 0 by the command, the value of the corresponding bit of x; if y is on by taking the 1 0, the result bits.

(1) If the bit is a 0 on the right side, the left remain unchanged.

(2) If the bit is a 1 on the right side, the left position zero.

2. XOR (^) [XOR]

  1. Go language, XOR is present as binary operators. However, as appears represented unary bitwise.
  2. As XOR binary operators, binary addition is not calculated, i.e. XOR, embodiment 0000 0010 0000 0100 = 0000 + 0110 = 6
  3. Remainder operator can only be applied to an integer: 9% 4 = 1
  4. Floating point division by 0.0 will return a result of endless, with + Inf representation;
func inf() {
	var a = 15.0
	fmt.Println(a / 0.0)  //+Inf
}
  1. ++;-- 在Go 中是语句,非表达式。
  2. rand.Seed(value) 函数提供伪随机数的生成种子,一般情况下都会使用当前时间(纳秒)做自变量。

  1. 深入学习GO语言 ↩︎

Guess you like

Origin blog.csdn.net/AMimiDou_212/article/details/100103494