This is a creation in Article, where the information may have evolved or changed.
Before starting a new chapter, take a look at the part of the previous article, first of all, let's see this code:
Package main Import ( "FMT" ) func main () { var x, y int=1,2 { var × int=2 { var x Int=3 FMT. Println (x, y) //Output 3, 2 if I have to comment out this line, guess what? Fmt. Println (x, y) //second output 2,2 } fmt. Println (x, y) //Last output
I have added to the notes, there is no need to be curious, the go language is so, this sudden out of the large structure into a complex sentence, the scale within the scope of the changes can only be in the volume, which involves the scope of the variable, in PHP is also a scope of the variable, But PHP doesn't make this kind of complex sentence when it encounters this sudden big parenthesis, so it doesn't affect the scope of the change, so please kan
Secondly, when we use global variables in the function of PHP, we usually introduce global variables into it, but in go language we don't need to be so troublesome, global variables can be used directly in function before function, without the need for extra description. , in go language, global variables are scoped to the current package, and the scope of local variables is limited to their large parentheses.
In the previous article, when I introduced the type, I mentioned a type of conversion, and here's how the go language is transformed into a digital type.
First of all, let's be clear that Go is type-safe, and it forbid that the implicit conversion of a type would result in a loss of precision, and that all types of conversion in the go language have to be made to be obvious.
Its obvious conversion method is actually very simple
Variable 1=< 1 type > (< variable 2>)
For example:
v1:=3.14
var v2 int
V2=int (v1)
In the example above, I converted the floating point type v1 to int. The value of V2 at this time is 3.
There are a few points to be aware of when you are switching:
type int can be converted to float type
Float type can be converted to int type
The string type can be converted to []byte and []int type
[]byte can be converted to string type
[]int can be converted to string type
Nothing else is possible.
This type of conversion can guarantee that the variable type is safe and will not be secretly transformed like PHP, for example:
Var_dump ("5" + "0");
This code is supposed to be two string strings ("50"), but PHP's implicit conversion changes his type to an int (5), which is not going to happen in go language, which is not the case in the word.
In addition to this type of conversion, go language also provides a standard package for the conversion of the digital type, the package name is StrConv, which provides the following methods:
Atoi (String) (Bool,error) receive string parameters to int type
Itoa (int) (BOOL) receives int type parameters As String
Formatbool (BOOL) string receives a reference to a string type
Formatfloat (float64, floating point format, output accuracy, [bit number 32,64]) string receives the number of the parameters as String type
Formatint (int64,[import 2,8,10,16]) string receive int type parameters are converted to String type
Formatuint (int64,[-2,8,10,16]) string ibid., receiving a notation int as a string type
Parsebool (String) (Bool,error) receive string parameters converted to bool type
Parsefloat (string, [bit 32,64]) (bool,error) receive string parameters converted to float type
parseint (String, [import 2,8,10,16],[bit 0,8,16,32,64]) (bool,error) receive string parameters to int type
Parseuint (String, [Advanced 2,8,10,16],[bit 0,8,16,32,64]) (Bool,error), the received string parameters are converted to the symbol int type
These methods are also useful in the conversion of a digital type, requiring extra attention.
So the next time we're going to talk about the process control of Go language, this part mainly involves if;switch;
First, let's look at the If section. First, let's look at the next code:
var a int a=xx () if a==1{ fmt. Println ("1") }else if a==2{ fmt. Println ("2") }else{ FMT. Println ("Other") }
On the upper side of the code is not difficult to find out that the if and other languages of the go language almost no difference, but note that it is not possible to have a small parenthesis in the sentence, and there is a left large parenthesis that must appear at the end of the sentence, and not be able to write a new line. Like this demo on the side of the page can't write this way:
var a int a=xx () if (a==1) //This is the first bug { //This is the second wrong FMT. Println ("1") } Note that these two points are almost there, except for the note above, there is a point to note, please look at the next side of the code: if A:=xx (); a==1{ FMT. Println ("1") } fmt. Println (a) //This is wrong because the variable a is a region variable of IF
In this demo, the variable A is placed in the if line, where a is the region variable of this if
To summarize, the IF in go language should be used to pay attention to three points is enough, the difference is: 1. The terms must not have a small parenthesis, 2. The left large parenthesis must be placed at the end of the sentence, 3. Other phrases can be placed on the left side of the commit language, and if there is a variable value, then the scope of the change is limited to if
Next, let's look at the switch usage of Go language, first we'll look at the next-generation code:
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 of the bundle")
In this demo on the top, if you look closely, you will find that the switch usage and other languages of the go language are not very good, but every case has no break, because the go language is not required to add a break, in the example above if the value of a is equal to 1, then the "1 End", see there is no need to join the break can automatically jump out of switch, then if the value of a and so on 4 will be output "45", this is because of the Fallthrough, Fallthrough can let case can be traversed, For the first time, the switch of Go language has a higher level of usage, at least this usage has not been seen in any other language before, and this usage allows switch to look at the next demo as if:
A:=XX () switch { ///Zoda must be the same line as switch case a<0: fmt. Println ("negative") case (a>0) && (a<=10): FMT. Println ("1-10") Case (a>10) && (a<=20): FMT. Println ("11-20") default: FMT. Println ("Other") }
On the next example, there is no sentence after switch, and the sentence sentence is taken to the end of the case, this is what I talked about on the switch to use as if.
So let's end up with Switch's attention: 1. The left large parenthesis must be the same line as switch, 2. You do not need to use break to jump out of case,3. If you need to penetrate the case, you have to add fallthrough,4. You can use it without sentence.
In the end, let's talk about the loop of Go language, where there is only one for use in the Go language, the white;do that are commonly seen in other languages. While;do. Loop;foreach;while and so on, this is a series of seven or eight bad loops that only need for in go is enough. And then we're going through a couple of demos to talk about how to do this. See the Code
For i:=0;i<10;i++{ //Note Do not add small parentheses, the left large parentheses must be in the same row of FMT. Println (i) }
This is a common use, except not to add small parentheses and the left large parentheses must appear at the end of the line, and other language without any difference in the process, so the needle for this kind of cycle I will no longer say. And then we'll look at the second usage:
I:=0 for ; i<10;i++{ FMT. Println (i) } fmt. Println ("Continue to output:", i)
In this example, the variable i is taken out of the for-loop definition, so on the for I don't need to be in the extra definition of it, just need a number of points, and then after the for loop I can also output the value of variable I, because this time the change is no longer specialized for the for. And then I'll see.
I:=0 for i<10{ FMT. Println (i) i++ } fmt. Println ("Continue to output:", i)
In this case, the third word is gone, I got the for inside, so I can omit all the points in the sentence. Isn't it interesting that it actually has been able to replace while. Maybe you can still feel the addiction, and then we'll get a much more ruthless
I:=0 for { if i>10{ break } FMT. Println (i) i++ } fmt. Println ("Continue to output:", i)
See, do. Loop or do. All of the while are replaced. Use this cycle to keep an eye out of the loop, otherwise it's not good to have a dead loop. What, you don't know how to write a dead circle? All right.
for { FMT. Println ("I Am a Dead Circle") }
It's so easy to see that you're dead. If you don't believe you can do it yourself and you will know the effect. Finally, there is only the foreach notation, look at the next example
i:=[]string{"AA", "BB", "CC"} for k,v:=range i{ fmt. Println (k,v) }
By passing this code, you can give the key names and values of the numbers to line by row.
And then we're going to summarize what go language's for loop needs to be aware of: 1. The terms can not be enclosed in parentheses, and the left is the same line as for the for, 2. If the terms are more than two, they cannot be omitted.
In addition, for the loop involves the use of break and continue, if you have a friend who can not understand their own web search, this is relatively simple here I no longer baggage described.
There is the go language added Goto, saying that I do not recommend you to use, in this way, I would like to mention that interested friends can do their own research.
Hope to download the article friends can leave an article out, http://see7di.cnblogs.com here grateful!
Just set up a QQ group, interesting talk about go language friends can join: 195112