In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
The knowledge points of this article "the representation of Go language variables" are not understood by most people, so the editor summarizes the following contents, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Go language variable representation" article.
Short form, using the: = assignment operator
We know that the type of variable can be omitted during variable initialization and can be automatically inferred by the system, so the declaration statement written with the var keyword is actually a bit redundant, so we can abbreviate them to a: = 50 or b: = false.
The types of an and b (int and bool) are automatically inferred by the compiler.
This is the preferred form of using variables, but it can only be used in the body of a function, not for the declaration and assignment of global variables. You can efficiently create a new variable called an initialization declaration using the operator: =.
Matters needing attention
If we cannot use initialization declarations for variables of the same name again in the same code block, for example: a: = 20 is not allowed, the compiler will prompt the error no new variables on left side of: =, but a = 20 is OK, because this is to assign a new value to the same variable.
If you use the variable a before defining it, you will get the compilation error undefined: a.
If you declare a local variable but do not use it in the same block of code, you will also get a compilation error, such as the variable an in the following example:
Example
Package main
Import "fmt"
Func main () {
Var a string = "abc"
Fmt.Println ("hello, world")
}
Trying to compile this code will result in an error a declared but not used.
In addition, it is not enough to simply assign a value, which must be used, so use the
Fmt.Println ("hello, world", a)
The error is removed.
But global variables are allowed to be declared but not used. Multiple variables of the same type can be declared on the same line, such as:
Var a, b, c int
Multiple variables can be assigned on the same line, such as:
Var a, b intvar c stringa, b, c = 5,7, "abc"
The above line assumes that the variables aforme b and c have been declared, otherwise they should be used as follows:
A, b, c: = 5,7, "abc"
The values on the right are assigned to the variables on the left in the same order, so the value of an is 5, the value of b is 7, and the value of c is "abc".
This is called parallel or simultaneous assignment.
If you want to exchange the values of two variables, you can simply use a, b = b, a, the two variables must be of the same type.
The blank identifier _ is also used to discard values, such as the value 5 is discarded in: _, b = 5,7.
_ is actually a write-only variable, and you can't get its value. This is done because in the Go language you have to use all the declared variables, but sometimes you don't need to use all the return values you get from one function.
Parallel assignment is also used when a function returns multiple return values, for example, val and error err are simultaneously obtained by calling the Func1 function: val, err = Func1 (var1).
The above is the content of this article on "the representation of Go language variables". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.