go -- Language Fundamentals

2020/02/23


Main Concepts Data Type

  1. 25个key words。变量命名以字母或下划线开头。但是不推荐_出现在中间。
  2. 声明和赋值:
    ex:var name string = “darcy”
        var name string–声明 name = `darcy`–赋值
        var name = `darcy`
        var (
             name1 = “darcy”//没有";"
             name2 = “lizzy”
             )
        firstName := “darcy”–声明and赋值(推荐);
        x,y,x := 1,2,3–多变量分别声明赋值
        x,y := y,x–exchange
go’s types basic aggregate reference interface
ex: numbers,strings and booleans arreys,structs pointers,slices,maps,functions and channels
  1. string中的"“和’’,``作用不同”/n/n"会被转义,``不会,而’'表示rune literal(码点字面量,类型int32)
  2. string是immutable. 变量名仅相当于一个指针, 所以也不能进行修改(s[0]=‘a’ ,报错,但是s += 'a’可)
  3. zero values:0,false,"",nil(for slice pointer map channel function.etc)
  4. 词汇基础
    注释:// /* */
    key words:break default func if .etc
    operator and punctuation:+ - & += := .etc
    boolean type: true false
    numeric type:int int8 uint float32 complex64 byte rune .etc
    array type: [10] int []32 byte
    slice type:make([]int, 50, 100)
    struct type: struct{string name, int age}
  5. operator orders:从上到下
    * / % << >> & &^
    + - | ^
    == != < <= > >=
    &&
    ||
    btw: &:bitwise AND
         |: bitwise OR
         ^: bitwise XOR
         &^: bit clear (AND NOT)
         <<: left shift【var x unit8 = 2;x = x << 1;fmt.Printf("%08b",x) 输出:00000100】
         >>: right shift
  6. 输出变量(print verbs):
    %v: struct
    %T: show type
    %d: uint int.etc
    %t: bool
    %g(%5.1f.etc ): float32 complex64 etc.
    %s: string
    %p: chan
    %pointer %P
    %b: 二进制表示
    %o: 八进制
    %x: 十六进制表示
    %q: []rune【fmt.Printf("%q", []rune(“nice”)) 输出:[‘n’,‘i’,‘c’,‘e’]】

other things

  1. fmt.Println(“good Day[5]”): 输出为D的ascii(68)
  2. fmt.Printf("%3d %8b %08b", 2, 2, 2)输出:
      2       10 00000010
  3. 和NaN作比较永远为false
  4. s := “hello好”;fmt.Println(len(s));fmt.Printlin(utf8.RuneCountInString(s)) 输出:9和6
  5. scope(作用域):
type local-level global-level
scope own block file:lowercase:anywhere in the file;package:uppercase:anywhere within the package
lifetime end of the enclosing block the entire execution of the program

{}in the proceding code is called a “syntactic block”,一个{}即为一块(stack)
当然还有lexical block,包括explicit和implicit block

  1. const (
    a = iota
    _
    b = iota
    );fmt.Printf("%d %d", a, b)
    输出:0 2

你可能感兴趣的:(golang)