This is a creation in Article, where the information may have evolved or changed.
1,IF Conditional Statement CONSIDERATIONS: Conditional statements do not need parentheses to enclose conditions (); The curly braces {} must exist regardless of the statement body. The left curly brace {must be in the same line as if or else, and before the conditional statement, you can add a variable initialization statement, use; interval;
2,slice can be directly used append to add elements such as append (slice,slice corresponding elements), you can also add slice directly in slice, but at this time the wording is append (Slice1,slice2 ... ), where Slice2 has ... Indicates that the SLICE2 is broken into elements and then added to the Slice1
3,break can be tagged to indicate a loop that jumps out of the corresponding label. A break look indicates that the outer loop is out of bounds.
Loop: For I: = 0, I < 5; i++ {for J: = 0; J < 5; J + + { If J >= 4 {break Loop } }}
4,go can be used as follows: "... type" means a parameter with a variable type, and an indeterminate parameter is essentially a slice type, so you can use range to take a value on its arguments. The following example.
Func myfunc (args ... int) { for _, arg: = Range args { fmt. Println (ARG) }}
There is no concept of constructors in 5.go languages, and the construction of objects is usually given to a global constructor, usually named after Newxxx, to represent constructors. Such as
Func newrect (x, y, width, height float64) *rect { return &rect{x, y, width, height}}
6. There is no strict separation of memory allocations for heaps and stacks in the go language, and it is absolutely no problem to return the address of a local variable in go, where the stored variables persist after the function returns. (note: Especially for programmers who have been turned over by C/T, the start is definitely not a good fit, but the go memory allocation frees up programmers, allowing programmers to focus on doing things without spending too much time on heap and stack memory allocations). More directly, In the go language, if a local variable is still used after the function is returned, the variable is allocated in memory on the heap instead of the stack stack. Detailed reference How does I know whether a variable is allocated on the heap or the stack?
7. Golang allows multiple return values, and the return value has two forms, one specifying the type, and one specifying the return value name. As shown below:
Func Sumandproduct (A, b int) (int, int) { return a+b, a*b} func sumandproduct (a, B int) (add int, multiplied int) {
add = a + b; multiplied = a * b;
When you define a function that returns a parameter variable, you can return directly without the variable name. Note: If the return parameter of the command has the same name as a variable in the function code block, he will be hidden and the return result will be displayed, and the explicit return would first modify the named return value before executing the DEFER delay statement. It is recommended to use a form that does not specify a return value name because the temporary variable name in the function and the specified return value name conflict are present.
8. Parameter variable parameters: Golang supports arguments, the category of all parameters in the variable must be the same, and must be the last parameter. Gets the variable parameter as a slice within the function. The function declaration is: func FuncName (args ... int) {} args ... int tells go that the function accepts parameter parameters. Use such as SUM (X[2:6] ...)
9. Defer and return execution order
When the function executes to the last, the defer statement is executed before the return statement is executed. So you can use this to carry out resource security shutdown, unlock, record execution and so on. Defer is the use of advanced post-out mode, this situation is consistent with the situation of the stack. NOTE: Defined defer delay operations, where a copy of the value occurs if a parameter is supplied, although these functions are executed at exit, the parameters used are copied at the time of definition, the principle of the copy and the assignment principle of the variable, and the Slice,map,channel is a pointer copy. Here's an example:
<span style= "color: #000000;" >package mainimport ("FMT") func main () {x: = 1defer func (a int) {//assigns the x=1 directly to a, although he is executed later. Fmt. Println ("A=", a)} (x) defer func () {fmt. Println ("x=", x)//X-Value to 2} () × x + + operation result after X + +: x= 2 a= 1</span>