Go language basics Tutorial: Process Control

Source: Internet
Author: User

Before starting a new chapter, let's review the previous article. First, let's look at this generation:

Package main import ("FMT") func main () {var X, Y Int = 1, 2 {var x Int = 2 {var x Int = 3 FMT. println (x, y) // first, output 3, 2. If I comment out a sentence for this line, Let's guess what will happen? } FMT. println (x, y) // returns 2, 2} FMT. println (x, y) // returns 1, 2}

 


As a result, I have already been added to the website. In fact, there is no need to be curious. In the go statement, this is the case, the sudden appearance of this kind of big data includes the structure of a joint sentence, and the changes in big data can only be within the variable content, this involves the scope of changes. In PHP, changes also have scopes, however, when PHP encounters such a sudden appearance, it will not constitute such a sentence, so it will not affect the scope of changes, please remember this
Second, when we use global changes in PHP functions, we usually use global to introduce global changes. However, we do not need to make that effort in go statements, global changes can be used directly in the function if they appear before the function, without the need for external descriptions, the role of global changes is the previous package, and the scope of local changes is limited to its large volume.

In the previous article, when I introduced the data type, I mentioned a data type classification section, here, I would like to explain how the go statement describes how to optimize the data type.
First of all, we need to know that go statements are classified security. It does not allow the precision loss caused by a number of classified classification statements, in the go scripting language, all types of classification functions must be implemented in simplified form.
Its Simplified compaction method is very simple.
Change 1 = <type of change 1> (<change 2>)
For example:
V1: = 3.14
VaR V2 int
V2 = int (V1)
In the previous example, I converted the V1 Floating type into int type. At this time, the value of V2 is 3.
Pay attention to the following points when using traditional encryption:
Int type can be converted to float type
Float type can be int type
String type can be [] Byte and [] int type
[] Byte can be string type
[] Int can be string type
None of the above.
This kind of metadata can ensure the security of the Changing type, and it will not be stolen like PHP. For example:
Var_dump ("5" + "0 ");
This statement should be equivalent to generating a string ("50") after two strings are connected in string mode. However, the PHP linear programming language has changed its class type, it becomes int (5), which cannot be generated in the go statement.
In addition to this kind of classification method, go language also provides a standard package for reading data, the package is named strconv. It provides the following methods:
Atoi (string) (bool, error) receives string values of int type
ITOA (INT type) (bool) receives int-type bytes as strings
Formatbool (bool) string receives the data distribution as string type
Formatfloat (float64, floating point format, precise output, [bits 32, 64]) string receiving Distribution
Formatint (int64, [in 2, 8, 10, 16]) string receives int-type data. The value is in string format.
Formatuint (int64, [hexadecimal,]) string is the same as above. If the string type is not received, the value of string type cannot be received.
Parsebool (string) (bool, error) receives a string of the bool type
Parsefloat (string, [number of 32, 64]) (bool, error) receives a string of Float Type
Parseint (string, [produced in,], [bits, 16, 32, 64]) (bool, error) receives a string of int type
Parseuint (string, [hexadecimal,], [bits, 16,32, 64]) (bool, error) is the same as above, and the received string contains a random int type

These methods are also very useful when performing numeric classification operations. Please pay special attention to them.

In the next time, we will mainly introduce the process control of Go statement. This part mainly involves if, switch, and cycle,
First, let's take a look at the if part. First, let's take a look at the next generation:

VaR A int A = XX () If a = 1 {FMT. println ("1")} else if a = 2 {FMT. println ("2")} else {FMT. println ("other ")}

 
It is almost no difference between the if statement of the Go statement and the other statement of the statement in the previous example, but pay attention to it, that is, the clause cannot appear in the clause, or the left clause must be at the end of the sentence and cannot be written to a new line. just like the demo in the previous example, it cannot be used as follows:

VaR A int A = XX () if (a = 1) // This is the first callback {// This is the second callback FMT. println ("1")} pay attention to these two points. In addition to the attention mentioned in the previous article, there are also some points worth attention, please refer to the pipeline generation: If a: = XX (); A = 1 {FMT. println ("1")} FMT. println (a) // This is temporary, because a depends on the region variation of if.

 
In this demo, variation A is placed in the same line of if. At this time, a changes to the region of this if.
In general, in the go statement, it is enough to pay attention to three points when using the SDK. The difference is: 1. the specified sentence cannot contain a small quotation, 2. the upper left corner must be placed at the end of the sentence. other sentence types can be placed on the left side of the submitted sentence. If a variable value is assigned at this time, the role of this variable is limited to if.

Next, let's take a look at the switch usage in the go statement. First, let's take a look at the substitution of the following statements:

VaR A int A = XX () Switch A {Case 1: FMT. println ("1") Case 2, 3: FMT. println ("2 or 3") case 4: FMT. println ("4") fallthrough case 5: FMT. println ("5") Default: FMT. println ("other")} FMT. println ("end ")

 
In this demo in the previous example, if you read it, you will find that the switch usage of Go statement is not similar to that of other statements, however, there is no break in each case, because the break does not need to be added in the go language, in the example above, if the value of a is equal to 1, the "1 End" will be generated. If there is no such value, you do not need to add break to automatically jump out of the switch, so if the value of a is equal to 4, a "45 end" will be generated. This is because of fallthrough, fallthrough can allow the case to be crossed, for the first time, the switch in go statement has a higher usage. At least I have not seen this usage in other statements before. This usage allows the switch to be used as if, let's take a look at the pipeline Demo:

A: = XX () Switch {// the upper left corner must be in the same line as the switch. Case A <0: FMT. println ("numeric") case (a> 0) & (A <= 10): FMT. println ("1-10") Case (A> 10) & (A <= 20): FMT. println ("11-20") Default: FMT. println ("other ")}

 
In the example of "fail", the sentence "cannot be judged after the switch, but the sentence" fail "is obtained after the case is obtained, this is the switch I used in the upload for use as if.
So Let's sum up the switch precautions: 1. the upper left corner must be the same line as the switch. do not use break to jump out of case, 3. if you need to penetrate the case, you must add fallthrough, 4. you can use the sentence as if.

Finally, let's talk about the statement of Go statement. In the statement of go, there is only one for statement. In other statements, we often see white; do .. while; do .. loop; foreach; while and so on are all gone. This series of 7-8 bad cycle methods only need for in the go statement. next, we use several demos to explain one by one how we can achieve this. check the token

For I: = 0; I <10; I ++ {// note that do not add a small margin, the left margin must be in the same line FMT. println (I )}

 
This is a common usage. Apart from adding a small margin and a left margin, it must appear at the end of the row, there is no difference with other programming languages, so I will not talk about this method again. next, let's take a look at the second usage:

I: = 0 for; I <10; I ++ {FMT. println (I)} FMT. println ("too many outputs:", I)

 
In this example, variable I is obtained beyond the for cycle definition, so on the for, I do not need to make a definition beyond the scope, only one Shard is enough. After the for statement ends, I can still export the variable I value, because the changes at this time are no longer due to. then

I: = 0 for I <10 {FMT. println (I) I ++} FMT. println ("too many outputs:", I)

 
In this example, the third clause does not exist and I have obtained the for content. Therefore, I can omit all the comments in the clause. is it interesting? In fact, this can already replace while. maybe you still don't feel better. Well, next time, let's make it harder.

I: = 0 for {if I> 10 {break} FMT. println (I) I ++} FMT. println ("too many outputs:", I)

 
No, do .. loop or do .. while is replaced. when using this method, you must control the jump out of the loop. Otherwise, it would be difficult to get a dead loop. what, you don't know how to avoid endless circulation? Okay.

For {FMT. println ("I am an endless loop ")}

 
No such order will lead to endless circulation. if you don't believe it, you can just click confirm again to see the effect. the last step is the foreach compile method. Let's take a look at the example of explain.

 i:=[]string{"aa","bb","cc"}    for k,v:=range i{        fmt.Println(k,v)    }

 
By using this generation, the dataset name and value of the Data Group can be output row by row.

Next, Let's sum up what we need to pay attention to in the for statement of Go statement: 1. the clause must be in the same line as. if the number of sentence terms exceeds two, the score cannot be omitted.
In addition, the for cycle will involve the use of break and continue. If you do not understand it, you can search for it on the Internet. I will not repeat it here.
In addition, the Go statement adds a goto parameter. If it is true, I am not recommended that you use it. In this article, I will mention that some interesting friends can study it on their own.


I hope that my friends can leave an article out, http://see7di.cnblogs.com is grateful here!

I have set up a QQ group. If you are interested in commenting on the go statement, you can join: 195112

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.