Summary of Go language learning (i)

Source: Internet
Author: User
Tags lua map data structure switch case
This is a creation in Article, where the information may have evolved or changed. Learn go. Record the
1), Package, import
These two features are available in many languages today (Java, C #). The introduction of the package in the C family is really a good thing, make the code organization clearer, import instead of include header file, completely eliminate the ills of the C family.

2) changes in the naming style of variables
Define variables:
var i int and c family syntax int i; a far cry, seemingly closer to Delphi, JavaScript, Scala style. Of course, in parsing, defining variables is easier to identify than the C-style definition.

type derivation:
type derivation is a must in many functional languages (Scala, F #, etc.), and C # also implements this function for the introduction of lambda expressions, but Java does not yet have it. It is important to note that type inference in go can only be used in functions. See See v the equivalent notation of ar i int=11 I: = one.

  Assign values to multiple variables at the same time:
Lua can assign values to multiple variables, go also has and syntax is almost the same drop a,b,c: = ----------if written as a,_:=1,2 then 2 is automatically discarded.
with this function exchange variable a sentence is done, a, b =b,a.

Define multiple variables:
If you are defining variables of the same type, you can var i,j int and C are almost int i,j;
If there are multiple different typesvariable that's it.
VAR (
I int
s string
)

To define an array:
Define the array with the definition variable slightly variable var array [8]int
You can also write this array: =[...] int{0,1,2,3,4,5,6,7} 。
Gets the array length len ( Arra y ), rather than the C-family Array.Length

3), go built-in data type
  slices:
The size of the array defined above cannot be changed. If it's going to be dynamic, it's going to be array: =make ([]int,8)Or This is
var array []int array =make ([]int,8)

[n:m] Accessors
[N:m] also applies to arrays. Using Array[n:m] (n, M is an array subscript) will return a slices from N to m-1.
If written as [n:] is equivalent to [N:len (array)]
 Array: =[...] int{0,1,2,3,4,5,6,7}
Array2: =array[2:4]//return {2,3}
               
The cap function
The CAP is used to get the capacity of the array.
 
Append function
Appends an element to a slices or array, resulting in a new slices.
              array: = [...] int{0,1,2,3,4,5,6,7} .
array1: =   array=append (array,8,9,10);// the added data (8, 9, 10) is a variable parameter

copy function
n = Copy (DES, SRC). n equals the number of copied elements.
            array: = [...] int{0,1,2,3,4,5,6,7} 。
Array1: = Make ([]int,5)
copy (Array1,array[0:5])

   Map:
The map data structure is built into go. Define a map that is such a var m map[ Ktype ] VTy PE . VType is the type of value stored by the Map keyword , k T ype is the data type of the keyword. Map creation is also a m=make that needs to be created with the Make function (map[ I NT ] Stri ng ) , where you create a keyword type of int, value a map of type string. If the value of the map is known, you can use this m:=map[int]string{
        1: "A",
2: "B",
3: "C",
... }
Note that each element in the map must be wrapped with a comma.
map adds element m[4]= "D";
Map to remove element Delete (m,4)
Map Judging the existence of val,exists: =m[4]

Many people have built in to go at the language levelSlices and map have a cheering attitude, go for built-in slices (map) also add keywords (go claims to be very stingy keyword drops). Lua, Python and other languages are built with a very distinctive data type, and go goes in one Direction (Ruby, Python developers are cool). Maybe the slices (map) that the go developer says is lightweight and fast.  
4), control statement
The Go Control statement is streamlined (removing the while, do) a lot more flexible.
A), if statement. If there is no parentheses inside the conditional expression, the parentheses are forced. Conditional expression of the change point if can have initialization statement
if a:=1; a==0{}
B), for statement
for-expression; Conditions Expression {} Typical C-family for
For condition {} C Family the while alternative
for{} dead Loop
The For and keyword range combine to iterate through arrays, slices, and maps. JS-like for In, Java for (Val:list.values ()), C # foreach.
For Key,value Range array{} C), switch. The obvious difference is that you don't have to thank Braek in the go switch case.
C1,case can have conditional expressions
A:=1
swich{
Case a>0&& a<10:
DoSomething
Default
DoSomething
}
C2,case Continuous Matching
Swich a{
Case:
DoSomething
Default
DoSomething
}
C2, Fallthrough. The New keyword Fallthrough lets the current case match to the next case:
Swich a{
Case 1: Fallth Rough
Case 2: Fallth Rough
Case 3: Fallth Rough
DoSomething
Default
DoSomething
}
The C3,GO switch has the same if statement Initialize Statement
Swich a:=1;a{}
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.