In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to define variable constant in Go language? I believe many inexperienced people don't know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
The go language does not support implicit type conversion, nor can aliases and existing types perform implicit type conversions.
Implicit conversion is not supported in go language
Variable declaration var v1 int var v2 string var v3 [10] int / / Array var v4 [] int / / Array slice var v5 struct {f int} var v6 * int / / pointer var v7 map [string] int / / map,key is string type, value is int type var v8 func (an int) int / / you can also declare the variable var (v1 int v2 string)
Declaring a variable does not require a semicolon as a Terminator
Initialization of variables var v1 int = 10 / / define and assign var v2 = 10 / / the compiler can automatically derive the type of v2 v3: = 10 / / the compiler can automatically deduce the type of v3
Combination of colon and equal sign: = declare and assign
The variable to the left of: = should not have been declared, otherwise it will result in a compilation error
Var an int
A: = 2
Results in a compilation error similar to the following: no new variables on left side of: =
Variable assignment var v10 int # define variable v10 = 1 # variable assignment
The go language provides multiple assignments, such as the following statement that exchanges I and j variables: I, j = j, I
In languages that do not support multiple assignments, it is necessary to introduce an intermediate variable to interact with the contents of two variables: t = I; I = j; j = t
Anonymous variable
When we program in a traditional strongly typed language, it often happens that we have to define a bunch of useless variables because the function returns multiple values in order to get a value when calling a function. In Go, this can be done by combining multiple returns with anonymous variables to avoid this ugly writing and make the code look more elegant.
Suppose the definition of the GetName () function is as follows, and it returns three values, firstName, lastName, and nickName:
Func GetName () (firstName, lastName, nickName string) {return "May", "Chan", "Chibi Maruko"}
If you only want to get nickName, the function call statement can be written as follows: _, _, nickName: = GetName ()
This usage can make the code very clear, basically blocking out the content that may confuse the eyes of the code reader, thus greatly reducing the complexity of communication and the difficulty of code maintenance.
Constant
Constants are values that are known and immutable during compilation. Constants can be numeric types (including integer, floating-point, and plural types), Boolean types, string types, and so on.
Literal constant
A hard-coded constant in a program
-12 3.14159265358979323846 / / constant of floating point type 3.2constant 12i / constant of plural type true / / constant of Boolean type "foo" / / string constant
The literal constant of Go language is closer to the concept of constant in our natural language, it is untyped. As long as this constant is within the range of the corresponding type, it can be used as a constant of that type.
Constant definition
Define constants by keyword const
Const Pi float64 = 3.14159265358979323846 const zero = 0.0 / / untyped floating point constant const (size int64 = 1024 eof =-1 / / untyped integer constant) const u, v float32 = 0,3 / / u = 0.0, v = 3.0 Multiple assignment of constant const a, b, c = 3,4, "foo" / / a = 3, b = 4, c = "foo", untyped integer and string constant / / constant assignment is a compile-time behavior, so the right value can also be a constant expression at compile time: const s = 1000 * 60 predefined constant
The Go language predefines these constants: true, false, and iota.
Iota is special and can be thought of as a constant that can be modified by the compiler. It is reset to 0 when each const keyword appears, and then the number represented by iota is automatically incremented by 1 every time iota appears before the next const.
Const (/ / iota is reset to 0c0 = iota / / c0 = = 0c1 = iota / / C1 = = 1c2 = iota / / c2 = = 2) const (u = iota * 42 / / u = = 0v float64 = iota * 42 / / v = = 42.0) after reading the above, have you mastered how to define variable constants in Go language? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.