Go language learning notes-expressions

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Golang keywords

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

Goang operator

Operator overloading is not supported. In particular, it is important to note that the "+ +", "--" is a statement rather than an expression.

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

Control flow

x := 0if n := "abc"; x > 0 {// 初始化语句未必就是定义变量,比如 println("init") 也是可以的。    println(n[2])} else if x < 0 {// 注意 else if 和 else 左大大括号位置。    println(n[1])} else {    println(n[0])}

Cycle

Supports three loop-side methods, including class while syntax.

s := "abc"for i, n := 0, len(s); i < n; i++ {// 常见的 for 循环,支持初始化语句。    println(s[i])}n := len(s)for n > 0 {// 替代 while (n > 0) {}    println(s[n])// 替代 for (; n > 0;) {}    n--}for {// 替代 while (true) {}    println(s)// 替代 for (;;) {}}

Rang

An iterator-like operation that returns (index, value) or (key, value).

            1st  value      2nd  value------------------+-------------------+------------------+-------------------string          index          s[index]    unicode,    runearray/slice       index          s[index]map            key           m[key]channel         element

You can ignore the unwanted return value, or use the special variable "_".

s := "abc"for i := range s {// 忽略 2nd value,支支持 string/array/slice/map。    println(s[i])}for _, c := range s {// 忽略 index。    println(c)}for range s {// 忽略全部返回值,仅迭代。    ...}m := map[string]int{"a": 1, "b": 2}for k, v := range m {// 返回 (key, value)。    println(k, v)}

Switch

A branch expression can be any type, not limited to a constant. Break can be omitted and automatically terminated by default.

x := []int{1, 2, 3}i := 2switch i {  case x[1]:    println("a")  case 1, 3:    println("b")  default:    println("c")}

If you need to continue to the next branch, you can use Fallthrough, but no longer judge the condition. Omit the conditional expression, which can be used when If...else if...else.

Goto,break,continue

Supports Goto jumps within a function. Label names are case-sensitive and no tags are used to raise errors. Mate tags, break and continue can jump out of multiple nested loops.

func main() {L1:  for x := 0; x < 3; x++ {  L2:    for y := 0; y < 5; y++ {      if y > 2 { continue L2 }      if x > 1 { break L1 }        print(x, ":", y, " ")    }    println()  }}

Attached: Break can be used for the for, switch, select, and continue can only be used for a for loop.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.