In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to use common functions in the GE language". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use the common functions in the GE language.
Ordinary function
In Go language, the definition format of a common function is func [function name] (input parameter) (output parameter), as follows:
Func callFuncA (x, y string) (s string, err error) {return x + y, nil} func main () {callFuncA ("fried", "fried fish")
A method named callFuncA is declared in the sample code, which is only allowed to be called within the package, so the first letter is lowercase.
It has two input parameters, x and y, both of type string. The output parameters are variables s and err, and the types are string and error, respectively.
In addition, when you return a value in the body of a function, you can also use a quick return method:
Func callFuncA (x, y string) (s string, err error) {s = x + y return}
The name of the variable declared when you output the parameter can be applied to its own function. So if you execute return directly, it implicitly returns the output variables that have been declared.
When a function is defined, its input parameters also support the syntax of variable parameters:
Func callFuncA (x... string) (s string, err error) {s = strings.Join (x, ",") return} func main () {fmt.Println ("fried", "fried fish"))}
If declared as x... string on the input parameter, it means that the variable x is a variable of type string, and multiple string parameters can be passed in when the input parameter is entered.
The format passed in by the variable is slice, which we will explain in later chapters, which you can understand as a dynamic array with no limit to its length:
[0: fried 1: fried fish]
Generally speaking, the common subsequent operations on variable variables are loop traversal processing, or stitching and other operations.
Anonymous function
The Go language also supports the declaration of anonymous functions by default, almost in the same way as ordinary functions:
Func main () {s: = func (x, y string) (s string, err error) {return x + y, nil} s ("fried", "fried fish")}
An anonymous function can be declared anywhere without defining a function name. If it is followed by () immediately after the function body, it will be executed immediately after the declaration:
Func main () {s, _: = func (x, y string) (s string, err error) {return x + y, nil} ("fried", "fried fish")}
In the use of all function classes, it is very important to understand the scope of function variables:
Func main () {x, y: = "fried", "fried fish" s, _: = func () (s string, err error) {return x + y, nil} () fmt.Println (s)}
The anonymous function has no formal parameters, and no corresponding variables are defined inside the function. At this time, it reads the values of the global x and y variables, and the output result is "fried fish".
Func main () {x, y: = "fried", "fried fish" _, _ = func (x, y string) (s string, err error) {x = "eat" return x + y, nil} (x, y) fmt.Println (x, y)}
The anonymous function has a visible parameter, but the variable x is re-assigned inside the function. So what is the value of the external output variable x? The output is "fried fish".
Why has the variable x been reassigned within the function, but still the value of the global variable x has not been changed?
The essential reason is that the scope is different, and the variable x modified inside the function is the local variable within the function. The external variables are global variables, which belong to different scopes.
Structural method
In the manner of combining a structure (struct), you can declare the method that belongs to that structure:
Type T struct {} func NewT () * T {return & T {} func (t * T) callFuncA (x, y string) (s string, err error) {return x + y, nil} func main () {NewT (). CallFuncA ("fried", "fried")}
The use of specific functions is the same as ordinary functions, there is no other difference.
The value-passing and reference-passing method calls related to the structure will be expanded in later chapters.
Built-in function
The Go language itself supports some built-in functions whose calls do not need to refer to third-party standard libraries. The built-in functions are used in conjunction with the regular use of the Go language, and the number is very small. As follows:
Used to get some types of length and capacity: len, cap.
Used to create and allocate certain types of memory: new, make.
Used for error handling mechanisms (exception panic, exception capture): panic, recover.
Used to copy and add slices (slice): copy, append.
Used for simple output information: print, println.
Used to deal with plural numbers: complex, real, imag.
For the actual usage scenarios of each built-in function, we will expand further in subsequent chapters, because each built-in function essentially corresponds to various types of usage scenarios.
At this point, I believe you have a deeper understanding of "how to use the common functions in the GE language". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.