In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "the definition of constants and variables in Go language". In daily operation, I believe that many people have doubts about the definition of constants and variables in GE language. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "the definition of constants and variables in Go language". Next, please follow the editor to study!
Constant (Constants) and iota
Constants contain data that does not change. Constants can only be of data type boolean, number (int/float/complex), or string.
How to define:
Const NAME [TYPE] = VALUE
TYPE can basically be omitted because constants are simple data types, and the compiler can infer its data type from the value.
For example:
Const Pi = 3.14159
Constants are evaluated during compilation, so the defined constants must be results that can be calculated during compilation. For example, it is wrong to call some run-time functions to generate the value of a constant, because these run-time functions cannot be called during compilation. After the value of the constant is defined, it cannot be changed during operation, otherwise an error will be reported.
Const c = 3: 2 / / correct const d = getNumber () / / error
The precision of the constant can be arbitrarily long, and the Go will not have the problem of precision overflow. And when a constant is assigned, if the value is too long, you can use the continuation character\:
Const Ln2= 0.69314718055994530417232121458\ 176568075500134360255254120680009 const Log2E= 1/Ln2 const Billion = 1e9
The overflow problem occurs only when a value that exceeds the precision of a variable is assigned to a variable in Go.
You can define multiple constants at once:
Const beef, two, c = "meat", 2, "veg" const Monday, Tuesday, Wednesday = 1,2,3 const (Monday, Tuesday, Wednesday = 1,2,3 Thursday, Friday, Saturday = 4,5,6)
Constants can be enumerated. After defining the following constant, Female represents the number 1.
Const (Unknown = 0 Female = 1 Male = 2)
You can use iota to implement enumerations, and iota itself is a constant defined in the builtin package with a value of 0, which is used to define the number of sequences in the constant, increasing from 0:
Const (a = iota b = iota c = iota)
When iota is called for the first time, the value 0 is generated, and when iota is called again on the new line, it automatically increments by 1, so the above axiom 0 # # 1 # # 2. The above constant enumeration can be simplified to equivalent form:
Const (a = iota b c)
Iota cannot be used during run time because it is a constant that begins with a lowercase letter and will not be exported. The following code will report an error: iota is not defined
Var an int = iota
Iota can also be used in expressions, for example, iota+50 means to add the current Ita value to 50.
Each constant block (const block) structure resets and initializes the value of iota to 0.
Func main () {const a = iota / / axi0 const b = iota+3 / / bao3 const cMagic d = iota,iota+3 / / cymbi0Ligue dong3 const (e = iota / / efug0f = iota+ 4 / / feng5g / / gongy6) println
Variable
Before using variables, there are two processes: declaration of variables and assignment of variables. Declaring variables is also often called "defining variables". Must be used after the variable is declared, otherwise an error will be reported.
Common ways to define variables:
Var identifier type
For example:
Var an int var b bool var str string / / or var (an int b bool str string)
When a variable is declared, the default 0 initialization is done, and the default 0 initialization value for each data type is different. For example, the 0 value of the int type is 0, the 0 value of the float is 0, the 0 value of the string type is empty "", the 0 value of the bool type is false, and the 0 value of the data structure is the 0 value of nil,struct, which assigns all fields 0.
A variable can get its value during compilation, but if the value assigned to the variable needs to be calculated at run time, it needs to be delayed until the run time to get the corresponding value.
Var an int = 15 / / good assignment during compilation var b int = 15 var b int 3 / good assignment during compilation var c = getNumber () / / assignment only during run
Declarations and assignments can be combined:
Var an int = 15 var I = 5 var b bool = false var str string = "Hello World"
When you combine declaration and assignment, you can omit the type section for values of simple data types, because Go can infer the type itself from the value:
Var a = 15 var b = false var str = "Hello World" var (a = 15 b = false str = "Hello World" numShips = 50 city string)
Because the data type is inferred, the type inference operation is done at run time.
When using the assignment of an inferred type, if you want to specify a specific type, you need to specify it explicitly. For example, the type of integer numeric inference is int, and if you want it to be saved to int64, you must explicitly specify the type:
Var an int64 = 2
To infer the type must be declared and assigned together, otherwise there is no value and cannot be inferred from the value. For example, var an is wrong.
In addition to the above inference method, the combination of declaration and assignment can also be achieved through the: = symbol, which can also be inferred based on the data type, even the var keyword is omitted:
A: = 50
However: = can only be used within the function code block, and an error will be reported if used in the global scope, because type inference is performed at run time, while the variable declaration part in the global scope is determined during compilation. For example, the following will report an error:
12a: = 10 func main () {println (a)}
A variable cannot be declared again after it is declared (except in a different scope), and then can only be assigned with =. For example, executing the following code will report an error:
Package main import ("fmt") func main () {x fmt.Println 10 fmt.Println ("x =", x) x fmt.Println 11 fmt.Println ("x =", x)}
The error is as follows:
1 command-line-arguments.\ test.go:8:3: no new variables on left side of: =
The error message is obvious,: = there are no new variables on the left.
If you take a closer look at the error message above, you will find that no new variables is plural. In fact, Go allows us to declare and assign multiple variables at once using: =, and the syntax is correct as long as there is any new variable on the left.
Func main () {name,age: = "longshuai", 23 fmt.Println ("name:", name, "age:", age) / / name is reassigned because there is a new variable weight weight,name: = 90, "malongshuai" fmt.Println ("name:", name, "weight:", weight)}
It should be noted that name is assigned the second time, and after Go infers the data type of the variable for the first time,: = is not allowed to change its data type, because only the first time: = is declared to name, and then all: = is a simple assignment to name.
For example, the following error will be reported:
Weight,name: = 909080
Error message:
.\ test.go:11:14: cannot use 80 (type int) as type string in assignment
In addition, variables must be used after declaration, otherwise an error will be reported, because Go is very strict with the specification. For example, the following defines weight but does not use it:
12weightname name: = 90, "malongshuai" fmt.Println ("name:", name)
Error message:
.\ test.go:11:2: weight declared and not used
Variable scope (scope)
The scope of the Go language is lexical, meaning that the location of the text segment definition determines the visible range of values.
Variables defined inside the function are local variables, and only those defined within the code block (such as {... CODE...}) are also local variables. Variables defined outside the code block disappear, and variables outside the function are package variables or global variables. They can be accessed by multiple files of the same package in the same directory (because only one package can be defined in one directory in Go). But a package can be divided into multiple files) if the name of a variable begins with a lowercase letter, other packages cannot access the variable. If the name of a variable begins with an uppercase letter, other packages can access the variable.
The variable names of different scope can conflict, but it is recommended that you name the variable in a unique way.
At this point, the study of "the definition of constants and variables in the Go language" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.