Go Language Basics: If,goto,for,switch

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

The difference between Go's If,for,switch and C

People who have a C foundation know that if, for, and switch conditional languages are bracketed. Basic format:

if{}for(语句1;语句2;语句3{}switch(语句){}

In go, these grammatical rules and C still have a difference, summed up as a sentence is to remove the parentheses. The syntax rules are as follows:

if 语句 {}for 语句1;语句2;语句3 {}//跟C一样,这三个语句可以省掉任何一个语句switch 语句 {}

The difference is actually very simple. Due to the absence of parentheses, they have more functionality with C.

If

If there is a strong place to declare a variable in the conditional judgment statement, the scope of the variable can only be within the logical block of the condition, and other places will not work.

if x:=getXdata(); x>10 {//x的作用域只在这个括号里有作用,除了括号就不起作用了。}

For

The C language has a while, Do-while Loop statement. Go is not, how to achieve it. See below:

sum:=1for ;sum<100;{}或者省略;forsum<100 {}

Haha, it's easy.

Goto

Same as the grammar rules of C.

func testFunc(){  ...Here: //以冒号结束作为标签  ...  goto Here//跳转到Here处
Related Article

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.