In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to use variables in Go language, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something from this article.
I. format of variable definition
The variable definition expression for C++ is as follows:
Type name = expression; for example int number = 10; / / represents a variable of data type int called number with an initial value of 10.
The variable definition expression for Go is as follows:
Var name type = expression, for example, var number int = 10 / / represents a variable of data type int called number with an initial value of 10.
Difference:
1.Cure + needs to add a; sign at the end of a sentence, which is used to indicate the end of the statement, while the Go language does not need the; sign, which indicates the end of the statement by a line break.
2. The format of statement definition for Type name + and Go is completely different. C++ is Type name = expression; Go and var name type = expression.
3. The initialization values of variables that are not assigned an initial value are not the same in Clipper + and Go languages.
1) if the initial value of C++ is not specified, for example, the value of int number; number may be 0 or random, depending on whether number is a local variable, a global variable or a static variable. If the local variable is a local variable, the value of number is a random value, and if the global and static variables are, the value of number will be 0.
2) if the initial value of Go is not specified, for example, var number int; will be initialized to 0. There are no uninitialized variables in the Go language. Go specifies that if the expression is omitted, initialize the value, the number corresponds to 0, the Boolean corresponds to false, the string corresponds to "", and it is nil for interface and reference types.
4. Go is more powerful because of the variable declaration of Cure + and Go language.
If the variables in C++ are not initialized, you can use the variable list, otherwise you can only have one variable per statement, for example:
Int a _ r _ b _ r _ c; / / where a _ r _ b _ c must be an integer, and the type must be consistent. Int a = 10; / / must be a variable and a statement.
Writing in Go is more flexible, such as:
Var int / / denotes that 0var is int data, and initializes it to 0var a dagger c = true, 10, "hello" / / omitting the data type, and amemery bpenc can be of several different data types, where an is Bool, b is int, and c is string.
There are two ways to declare variables and short variables in the 5.Go language, while there are only variables in C++
The type of the short variable declaration format: name: = expression / / name is determined by expression. Because it is short and flexible, it is mainly used for the declaration and initialization of local variables.
Variables declared by var are usually local variables that are inconsistent with the type of initialization expression, or where variables are assigned later.
1) I believe that the main reason for the existence of short variable declarations is that multiple types of variables in Go language can be assigned on the same line, for example, aline b: = 1 # true / / where aline b can all be new variables and is not initialized, or a new variable can be found in aline b. The var variable does not support the latter very well, and it must be two lines to guarantee it, as shown below:
Var false b = 0, false / / step1:a, b must be defined first. True b = 1, true / / step 2: a meme b is being assigned
A short variable can be expressed with only one sentence.
2) _ represents a variable that is defined but not used in a short variable, which is often used when returning multiple values, one of which is not used. Note: it is defined in Go that variables must be used, otherwise there will be a compilation error. ) for example:
_, a: = func () / / where func has two return values, the first of which is not used.
Scope and life cycle of variables
Before we talk about the difference between C++ and Go, let's take a look at what scope and lifecycle are.
Scope: exists in the compilation phase and is used to indicate the scope of a variable or method, usually the part in curly braces, called a program block, which is a scope.
Life cycle: the concept of the execution phase, from the time period in which an instance of a variable is created to the time that the variable is released.
Difference:
1. Under the concept of scope, variables declared within {} can be used in this code segment, and variables with the same name generated outside {} will be overwritten.
Go language, under the concept of scope, is no different from C++ language. However, there is one thing to note about the use of short variable declarations. For example:
Var num intvar b string {a, num: = false, 5 / / num here will be considered to be a new variable b = "hello" / / b here is the same variable as b outside {}}
After reading the above, do you have any further understanding of how to use variables in the Go language? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.