Golang Notes

前端之家收集整理的这篇文章主要介绍了Golang Notes前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

2 Program Structure

2.1 Names

  1. 可见性

    1. 1)声明**在函数内部**,是函数的本地值,相似private
    2. 2)声明**在函数外部**,是对当前包可见(包内全部.go文件都可见)的全局值,相似protect
    3. 3)声明**在函数外部且首字母大写**是全部包可见的全局值,相似public
  2. 命名风格

    写短语要么全部大写。要么全部小写。比如htmlEscape,HTMLEscape,不能写成HtmlEscape。

2.2 Declarations

A declaration names a program entity and specifies some or all of its properties.
There are four major kinds of declarations: var,const,type and func.

Var/:=

Var

  1. var@H_404_32@ name type@H_404_32@ = expression

1.If the expression is omitted,the initial value is the zero value for the type:

Type Zero Value
numbers 0
boolean 0
strings “”
interfaces nil
reference types (slice,pointer,map,channel,function) nil
aggregate types (array,struct) the zero value of all of its elements or fields

2.function 内声明的var一定要用上,不然会报错;import的package一定要用上,不然会报错;const,type,func则不会发生上述问题。

    3.
  1. var@H_404_32@ i,j,k int@H_404_32@ // ok@H_404_32@
  2. var@H_404_32@ i,k = true@H_404_32@, 2.3@H_404_32@,"four"@H_404_32@ // ok & type(j)为float64@H_404_32@
  3. var@H_404_32@ i bool@H_404_32@,j float64@H_404_32@,k string@H_404_32@ = true@H_404_32@,"four"@H_404_32@ // error@H_404_32@

Short Variable Declarations (:=)

Within a function,an alternate form called a short variable declaration may be used to declare and initialize local variables.

  1. name := expression

1.

  1. i,j := 0@H_404_32@, 1.0@H_404_32@ // ok@H_404_32@

2.声明语句中必须要有新变量声明

  1. // 声明新变量 in,err@H_404_32@
  2. in,err := os.Open(infile)
  3. // 声明新变量 out@H_404_32@
  4. out,err := os.Create(outfile)
  5. // 而下面两个语句则不合法@H_404_32@
  6. f,err := os.Open(infile)
  7. f,err := os.Create(outfile) // compile error: no new variables@H_404_32@
  8. // 应该改为@H_404_32@
  9. f,err = os.Create(outfile)

Pointers

A pointer value is the address of a variable.
Pointer就是address的Type。

  1. x := 1@H_404_32@
  2. p := &x
  3. fmt.Println(*p) // "1"@H_404_32@
  4. *p = 2@H_404_32@
  5. fmt.Println(x) // "2"@H_404_32@

1.一个被引用的局部变量,即使在函数调用后仍然不会被回收:

  1. var@H_404_32@ p = f()
  2. fun f() *int@H_404_32@{
  3. v := 1@H_404_32@
  4. return@H_404_32@ &v
  5. }
  6. // Each call of f returns a distance value:@H_404_32@
  7. fmt.Println(f()==f()) // "false"@H_404_32@
  1. copy a reference types (pointer,slices,maps and channels) <=> create new aliases or ways to identify the same variable.

关于new

Another way to create a variable is to use the built-in function new. The expression new(T) creates an unnamed variable of type T,initializes it to the zero value of T,and returns its address,which is a value of type *T.

  1. p := new@H_404_32@(int@H_404_32@)
  2. fmt.Println(*p) // "0"@H_404_32@
  3. *p = 2@H_404_32@
  4. fmt.Println(*p) // "2"@H_404_32@

1.new不需要delete
2.Each call to new return a distinct variable with a unique address. However,there is one exception to this rule: two variables whose type carries no information and is therefore of size zero,such as struct{} or [0]int,may,depending on the implementation,have the same address.

  1. p := new@H_404_32@(int@H_404_32@)
  2. q := new@H_404_32@(int@H_404_32@)
  3. fmt.Println(p==q) // "false"@H_404_32@

3.The new function is relatively rarely used because the most common unnamed variables are of struct types,for which the struct literal Syntax is more flexible.

Lifetime of Variables

a package-level variable: the entire execution of the program (即使是被import的package中的package-level variable);
a local variable have dynamic lifetime: a new instance is created each time the declaration statement is executed,and the variable lives on until it becomes unreachable,at which point its storage may be recycled.

1.Golang的垃圾回收机制

2.Golang中variables哪些放在栈上,哪些放在堆上,不取决于它的声明方式是var还是new。

const

type

func

用例

  1. 读取/设置命令行的flags以及arguments。(P33)

猜你在找的Go相关文章