Date: January 1, July 21, 2014
I. Control Structure 1. There are only several control structures in go. It does not have do or while loops, and has for, Flexible switch statements and if, in the switch, you can accept initialization statements as optional as for. In addition, go also provides select statements for Type Selection and multi-channel communication adapters. The syntax of the Go control structure is different from that of C. It does not require parentheses, but the statement body must always be included in braces. 2. Control Structure Syntax 1) If-else (1) if followed by a single condition. For example, if X> 0 {// {must be in the same line as if. This is specified by go syntax, if the line feed is written, return y} else {return x} (2) if is returned and the initialization statement is accepted, for example, M1: = 7 if N1: = 9; n1> = m1 {FMT. print ("N1> = m1")} (3) if followed by a logical operator such as: if true & True {FMT. println ("true")} in the IF statement body, the statement body ends at break, continue, Goto, and return, which is no different from other languages. 2) goto go supports the GOTO statement, but be careful when using it. For example, func gotodemo () (string) {I: = 0 gotohere: // This is a tag defined in the function. Note that it is case sensitive. If I> = 5 {return "OK"} FMT. println (I) I ++ goto gotohere // goto jump to the function-defined tag} 3) IN THE for go language, the for control structure is relatively powerful, it can complete the for function in the C language and the while function. Of course, I don't know why the designers of this language need to change our general usage habits in other languages, but I think I like this style. There are only a few words. If I can achieve multiple effects through one keyword, why should I add another keyword? Google's design philosophy for the go language and its own compliance with"
Less = simpleThe concept is consistent. Three Forms of for loop: (1) For Init; condition; post {}// same as for (2) for condition {}// same as while (3) for {} // infinite loop 4) Break and continue break are used to terminate the loop, and continue is used to terminate the current loop for example: func breakdemo () {for I: = 0; I <10; I ++ {if I> 5 {break // The End of the entire loop} FMT. println (I)} Tag: for I: = 0; I <10; I ++ {if I> 10 {break tag // terminate the loop with the tag name} FMT. println (I)} func continuedemo () {for I: = 0; I <10; I ++ {if I = 2 {continue FMT. println ("after continue") // not executed} FMT. println ("continue")} 5) rangerange can be used for loops. Range is an iterator. When called, a key-value pair is returned from its loop content, range returns different items based on different content. For example: func rangedemo () {list: = [] string {"A", "B", "C"} for K, V: = range list {// use range to cycle it. In each iteration, range returns the serial number of the int type and the value of the string type FMT. print ("k =") FMT. println (k) FMT. print ("V =") FMT. println (v)} print: K = 0 V = ak = 1 V = BK = 2 V = C when looping slice or array, range returns the sequence number as the key, the content corresponding to the serial number is used as the value. The variation range of K value in the above loop is 0 ~ 2. The V value ranges from A "~ "C ". Range can also be directly used in string traversal. the string is broken into independent Unicode characters, such as: for POs, char: = range "ABCD" {FMT. printf ("character '% C' starts at byte position % d \ n", Char, POS)} print: character 'a' starts at byte position 0 character 'B' starts at byte position 1 character 'C' starts at byte position 2 character 'D' starts at byte position 3 6) the switch of switchgo is very flexible, and the expression does not need to be a constant or integer. the execution process goes from top to bottom until a matching item is found. If the switch does not have an expression, it matches true. For example: func switchdemo () {Len: = 4 switch Len {Case 0: println ("Len = 0") Case 1: println ("Len = 1") fallthrough Case 2: println ("Len = 2") Case 3,4: println ("Len = 3 or Len = 4") Default: println ("default")} Now, we will analyze the Len value in the switch: (1) if Len: = 0, print the result: Len = 0 (2) if Len: = 1, print the result: len = 1 Len = 2 Why can't case 2 be executed? This is because we use the fallthrough keyword, which forces the statement in Case 2 to be executed. (3) If Len: = 3 or Len: = 4, the printed result Len = 3 or Len = 4 is separated, the relationship between each condition is "or. (4) If Len is not 0 ~ 4, the statement in the default condition is executed and the default value is printed. 2. the built-in functions provide a few built-in functions in go. They can be used without importing any packages. the built-in functions in go are listed as follows: close: used for Channel Communication. Use it to close the channel. Delete: Used to delete instances in map. Len and CAP: can be used for different types. Len is used to return the length of strings, slice, and arrays. New: used for various types of memory allocation. Make: Memory Allocation for built-in types (MAP, slice, and channel. Copy: used to copy slice. See "slice" in this Chapter ". Append: Used to append an slice. See "slice" in this Chapter ". Panic and recover: used for exception handling. Print and println: are underlying print functions that can be used without introducing the FMT package. They are mainly used for debugging. Complex, real, And imag: all used to process plural numbers. With the simple example given previously, we don't need to discuss the plural.