Go core development study notes (b) - constant and variable

constant

  1. Constant use const modified C-like language define.

  2. Constants must be initialized when defined, the syntax: const <constant name> [type] = xxx

  3. Constants can not be modified.

  4. Constants can only be assigned bool, numeric, string.

  5. Constant Type inference computing assignment is possible: const num = 4/2

  6. Writing means B:
    const (
    A = 0 A = Itoa //
    B // A +. 1. 1 =
    C = 2. 1 // B +
    D, E
    )

    About iota Description:

    package main
    
    import "fmt"
    
    func main() {
    	const (
    		a = iota
    		b
    		c
    		d,e = iota,iota  //每一行增加1,同行所有数据一致,不++
    	)
    	fmt.Println(a,b,c,d,e)
    }
    
  7. Named constants is to control access by the first letter of the scope of the case.

variable:

  1. The basic unit of the program.
  2. Variable corresponds to a data memory space in memory, the value of a variable to be accessed by the variable name.
  3. Variables: & assignment statement using ----> var <var_name> <var_type > [= xxx],
    for example: var num int = 200 or var num int; num = 200 of the two children is one thing.

Note the use of variables (important):

  1. All code must be loaded into memory to run, variable represents a memory storage area.

  2. The region has its own name and type.

  3. Variable assignment process, var num int: defines an int, num variable name, the default is 0, num = 200: 200 0 replaces the memory after the assignment, thereby completing the assignment.
    Behind fmt.Println (num) is printed out at the console variable value

  4. Type variable assignment, observed by the code segment below and comments, in particular third form:

    package main
    
    import "fmt"
    
    func main() {
    
    //如果i不赋值,那么int默认值就是0
    var i int
    fmt.Println(i)
    
    //不指定num的变量类型,通过赋值则Go做出了类型推导int
    var num = 200
    fmt.Println(num)
    //同理,num1如果是赋予小数,那么就是float了
    var num1 = 10.5
    fmt.Println(num1)
    
    //var都不写,变量这种方式都不写,使用"<var_name> := xx",这样也叫作声明变量
    name := "durant"
    fmt.Println(name)
    }
    
  5. Disposable declare multiple variables:

    package main
    
    import "fmt"
    
    func main() {
    //一次性声明多个变量传统写法
    var st1 int
    var st2 int
    var st3 int
    fmt.Println(st1,st2,st3)
    
    //简化1
    var st4,st5,st6 int
    fmt.Println(st4,st5,st6)
    
    //上述两种方法都只能定义一种类型,所以做如下类型推导
    var st7,st8 = "xiaoming",11
    fmt.Println(st7,st8)
    
    //类型推导2
    st9,st10,st11 := "alibaba","tencent",666
    fmt.Println(st9,st10,st11)
    }
    
  6. Global variables:

    package main
    
    import "fmt"
    //全局变量定义传统
    var name1 = "james"
    var age1 = 34
    var gender1 = "male"
    
    //全局变量定义美观版本
    var(
    name2 = "durant"
    age2 = 32
    gender2 = "male"
    )
    //尝试使用 name3,age3,gender3 := "adtkb",24,"male" 是不可以的,所以定义全局变量只有两种
    
    func main() {
    fmt.Println(name1,age1,gender1)
    fmt.Println(name2,age2,gender2)
    }
    
  7. Only the variable data values ​​changing within the range of the same type, can not be changed by the int str

    package main
    
    import "fmt"
    
    func main() {
    //变量一定以最后一次赋值为基准
    var num int
    num = 20
    num = 30
    num = 40
    fmt.Println(num)
    
    //不可以改变数据类型,num=1.5这个也是不合规的
    //var num int
    //num = 20
    //var num = "alibaba"
    //fmt.Println(num)
    //这种肯定不可取
    
    }
    
  8. Variable in the same scope must be unique, it goes without saying, can not the same scope there are two variables the same name and different variable values.

  9. Variable three elements: variable name, variable value, variable data types

  10. If only variable, no copy, then the Go compiler will use the default value,
    int default value is 0, integer.
    The default value string "empty string.
    float32 default value is 0, single decimal precision.

Additional knowledge:

Common DOS command:
directory operations:
the dir // check the current directory of all files
cd / dd: // switch to d drive
cd d: \ test // switch to the test directory d of disc, where the relative and absolute paths directed to
cd ... // switch to the parent directory
md d: \ test1 // Create a new directory
md d: \ test3 d: \ test2 // Create multiple directories
rd d: \ test2 d: \ test3 // delete multiple directories (empty dir)
RD d: \ test1 // delete individual directories (empty dir)
RD / q / S // test1 directory and delete a single internal content, / q: silent mode does not ask, / s: level (recursively) delete all similar to the linux -r

File operations:
echo xxx> d: \ test.txt d // create a txt file disk, internal write xxx, xxx not described as a string type "xxx", enter what, which is what the text
copy test.txt d: \ shit \ test1.txt // copy the test.txt file in the root directory d to d : \ under shit, and rename test1.txt, provided that the destination folder must exist
move test.txt d: \ shit // move the test.txt file in the root directory d (cut) to d: \ under shit, provided that the destination folder must exist, or rename
del test.txt // delete a file

Other operations:
CLS // clear screen, similar to the Clear
Exit // exit cmd

Program "+" is used:

  1. Both sides are numeric, do adder.
  2. On both sides of the string, do string concatenation.

Pieces Code Example:

package main

import "fmt"

func main() {
	//整数类型
	i,j := 10,20
	var res = i + j
	fmt.Println(res)

	//小数类型
	k,l := 10.6,20.3
	var res1  = k + l
	fmt.Println(res1)

	//整数加小数类型没法做,会报错,告诉mismatch int float64 待解决

	//字符串类型
	str1,str2 := "very","good"
	var res3 = str1 + str2
	fmt.Println(res3)
}
Published 50 original articles · won praise 18 · views 4028

Guess you like

Origin blog.csdn.net/weixin_41047549/article/details/89540418