This is a creation in Article, where the information may have evolved or changed.
Now the series has begun to polarize. Like the thought style relaxed, look not sleepy. On the contrary, some people would like to write the Luo Li Bar, excessive. So good-natured reminder that there is not only technical language, but also jokes. Concentrate on the technology, don't point it! Don't blame me for not reminding you! Almost forgot to say, copyright does not, reproduced arbitrarily, do not delete the mailbox (ztao8607@gmail.com)
I admit that the title of the article is getting more and more ridiculous. From the beginning of the "installation of Golang", "decryption Golang engineering structure" This kind of good title, now has slowly evolved into "Victoria's Secret", "We are gen Y" this kind of a rather non-technical meaning of the title. To tell the truth, I can't make up any more.
Come to the lyrics to see if you can recall:
Dare to wear tiger skin in the King Yang Gang. Set a female wolf in a broomcorn. The left hand takes the knife (casa ~), the right hand takes the gun (PA ~). He is the absolute idol of cockroaches. Dare to go over the imperial dining Room in the palace. can also go to your home to sweep. Even if the shoe board took a shot of the death of the dog. Death is a cool thing to die for.
In the morning, I had no intention of hearing the lyrics of the < Spy, Xiao Qiang >, and feeling that I was back at the university. I do not know who is going to enter the second half of the youth, is not all like nostalgia. My Gen Y memory still stays in old Beijing yogurt, Beijing instant noodles, Laser Video hall, Sega game machine. In addition to 28 big bars of bicycles, is the roadside game room. That will be the Internet cafes are LAN in play Red Police, CS just out name for counter-strike. Sand dunes can also be found, but the most played is the Jin Yong Group of the Grand biography.
It was in these games that I was filled with curiosity about the computer, and I was dreaming of how good it would be if I could write myself a game one day. Ideals have always existed, but reality has been destroyed. Looking at the side of the next, after 00 has gradually carried up the industry's girder, only to their exclamation of a sentence:
In the youth queue, we are already at the end of the line. Well done, one day, let's meet at the end of the team.
Feeling has finished, start to the point. In the Tantra section, we talk about how the functions are used, and this section talks about variables scoped things.
Variable scope
To synthesize the previous description, we can draw a conclusion that there are variables in any language. It is also concluded that as long as there are variables, there will be variable scopes. Like every country has laws, and every law has its limits. The simplest example is that Chinese law does not control the United States. So this section is talking about variables scoped to those things.
Among Golang, variables are divided into three categories:
- Local local variables
- Global variables
- Parameter variables
Local local variables, as the name implies, are only valid locally. In Golang, the local definition is distinguished by function or semantic block. Whenever a variable is defined in a function, or a variable defined in a semantic block is a local variable.
Function definitions are easy to understand, such as the following example:
func Test(){ str := "This is a local var" ....}
STR is a variable that is defined inside the test function, so it is a local variable. The following is an example of a semantic block:
func Test(){ for{ str := "This is a local var in block" ... }}
You can see that STR is defined inside for this semantic block, so STR is also a local variable. So where is the scope of the local variable? The law of which country, where the variable is defined, takes effect within the current scope.
In example one, STR is defined in the test function, so str is unblocked inside the test function. After the test function is out, the STR variable cannot be used. In the same vein, in example two, STR is defined inside a for loop, so STR can only be used for internal use. Once the For loop is out, it is no longer possible to access Str.
Yes, you will certainly ask, what if there is a duplicate variable? First of all, I suggest, try not to appear duplicate name. Try not to dig your own holes. But not dig, that Golang will replace the variable according to the principle of the current scope precedence. For example, the following example:
package mainimport ( "fmt")func main() { str := "local var" if true { str := "block var" fmt.Println(str) } fmt.Println(str)}
A str variable is defined in the main function, and a str variable is also defined in the If semantic block. According to the principle of the current scope precedence, when the program runs inside the IF, str in the if is substituted for the external str variable value. So first output "block Var". When the if range is out of the box, the inside STR is invalidated and cannot be accessed externally. So we end up using the STR variable of the main function scope, so we output "local var".
So the final output
block varlocal var
Or that sentence, the code is best not to use fancy things, write clearly and plainly the best. Generally do not dig a hole, to dig a big hole.
corresponding to the local variable is the global variable. As long as it is defined outside the function, it belongs to the global variable. For example, the following example:
var g intfunc main(){ a := 10 b := 10 g = a+b ...}
Note that you must not g: = A+b. Because we have said that variables are declared in the form of "Var" and ": =" two. If you accidentally wrote a "G:=a+b". Although it has no effect on the final result, the meaning has changed, and it becomes a re-creation of a G variable in the main function.
Although global variables are also variables, the declaration of global variables can only be achieved by using Var. For example, the following is legal:
var g int或者var g = 40func main(){...}
And the following is illegal:
g := 40func main(){}
Do not ask what, the rules are rules, compliance is good. The scope of global variables is, of course, global, but this global refers to the package. It is freely accessible within the current package, and if you want to get other package access, you need to follow the Golang principle and capitalize the first letter. This will allow the other package to access.
What if you encounter a problem with the same name? The Golang is also handled in accordance with the scope precedence principle, such as the following example:
var g = 40func main(){ a := 10 b := 10 g := a+b ...}
Note that in the main function, there is also a G variable, but the G variable is declared by ": =". So according to the scope first principle, at this moment of g=30. The global g=40. If this is not understood, write two lines of code, run a moment to see.
The last one is parameter variables. A parametric variable is a special variable that is not a local variable or a global variable (a bit of nonsense, if it belongs, it will not be listed separately). It is in between, scope is the function scope, first look at the following example:
func Test(str string){ i := 10 ....}
From the above analysis, I is a local variable, and STR is a parameter variable. At the same point as the local variable, its scope is the same as the function range. But there are two different places:
- Parameter variable assignment is done externally, Lenovo < Vimi > that section, we explain the function, how to assign a value to the parameter. You can know that the assignment is done at the time of the call.
- Parameter variables can be declared after they are not used. This is especially true if a local variable is declared but is not used throughout. You will be prompted to delete this variable at compile time. parameter variables, regardless of whether the function is used inside, are not prompted.
So you can treat local local variables as parameter variables, local local variables applicable to the law, parameter variables also apply.
After writing the variable, look at the content is not much. So write a little more, anyway, there is no publishing house manuscripts, will not be bound by the outline, where to write to think.
Below enter the usage scenarios for Golang common types
String
In each programming language, a string is a built-in data type, because it is too important. As mentioned before, the string essence of Golang is a character array ([]byte).
Creating a string is simple, and it has been used many times:
var greeting = "Hello world!"或者greeting := "Hello world!"
Golang uses UTF-8 encoding to handle strings by default, so you don't have to worry about Chinese character coding problems. As a modern language, Golang does not limit the length of a string, theoretically, as long as the memory is large, the string can be how large. But still the spirit of "no Zuo no die" concept, don't put so long string, no one likes to read in the code of fiction.
Golang specifically has a strings package (https://golang.org/pkg/strings/) to handle a variety of common string problems. For example, find, splice, judge, case conversion and replacement, copy and so on common functions.
String is important, but there is really nothing to talk about. Most of the problems can be solved through the strings library, if there is a problem, take a look at the library document it. Although this series is an introductory tutorial, but everything is finished, very unrealistic. If it is finished, it becomes Golang Chinese document.
So this section of string, remember that there is a strings library is enough.
If you are Gen Y, you can still have such a strong thirst for knowledge. If you are not Gen Y, cherish the day ahead. It's time to play and the splurge is wasted. Don't wait for the day to boil the hairline to the ear, only to regret not to have a good sister.