TreeviewCopyright © aleen42 all right reserved, powered by aleen42

表达式

保留字

break default func interface select
case  defer go map struct
chan  else goto package switch
const fallthrough if range type
continue for import return var

运算符

+ & += &= && == != ( )
- | -= |= || < <= [ ]
* ^ *= ^= <- > >= { }
/ << /= <<= ++ = := , ;
% >> %= >>= -- ! ... . :
&^ &^=

和其他语言的差异:

  • bit clear 位运算符(AND NOT)是Go特有的,其形式为 a &^ b
  • Go中自增和自减不再是运算符,只能作为独立语句,独占一行,相比C/C++也不支持运算符重载
  • Go没有 "~",取反运算使用 "^"
  • 支持 字面量 初始化

控制流程

if

x := 0  
if x > 0 {  
   // ...  
} else if x < 0 {  
   // ...  
} else {  
   // ...  
}

// 支持初始化语句
if _, err := strconv.Atoi("sd"); err != nil {  
}

for

Go中没有while循环,do while循环等,统一使用 for 实现。

// 最常见的循环
for i := 0; i < 3; i++ {
}

// 类似 while x < 10 或 for ; x < 10; x++ {}
for x < 10 {
    x++
}

// 相当于 while true
for {
    if x > 10 {
        break
    }
}

for range

Go提供了 for range 表达式支持,可以方便的遍历切片、channel、map等

Copyright © CoffeeChat 2020 all right reserved,powered by Gitbook该文件修订时间: 2023-05-31 16:34:29

results matching ""

    No results matching ""