In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail about the basic definition and call of functions in the Go language. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Almost all programming languages support functions. The purpose of writing functions is to decompose complex problems into a series of simple tasks. In addition, the same function can be reused many times. This structure is very important in process-oriented functional programming. For the same class C language, the basic structure of Go language function and PHP function is similar, but the specific details are different. In Go language, the basic composition of function is: keyword func, function name, parameter list, return value, function body and return statement. As a strongly typed language, whether it is a parameter or return value, you have to declare its type when defining the function.
In the Go language, there are three main types of functions:
Ordinary function
Anonymous function (closure)
Class method
We will unveil the Go function, starting with the basic definition and invocation of ordinary functions.
Function definition
In the previous first Go program tutorial, we showed you the basic definition of a function and an example of calling it. Here we use the simplest addition function to explain in detail:
Func add (a, b int) int {return a + b}
If we call add (1,2), the return result is 3:
Func main () {fmt.Println (add (1,2)) / / 3}
Let's take the add function as an example to introduce the definition of the function. If the parameter list of the function contains several parameters of the same type, such as an and b in the above example, you can omit the type declaration of the previous variable in the parameter list and keep only the last one, as shown below:
Func add (a, b int) int {/ /...}
The parameter list is followed by the type of the function's return value (this is the same as the Go variable, putting the type last, unlike other static strongly typed languages such as Java and C in the way the function is declared:
Func add (a, b int) int {/ /...}
In addition, the Go function also supports multiple return values, which we will discuss later when we introduce the return values of the function.
Function call
Function calls are very convenient. If you are in the same package (that is, defined in the Go file in the same directory), you can simply call:
Func main () {fmt.Println (add (1,2)) / / 3}
If you are in a different package, you need to import the package in which the function is located before calling the function. For example, we put the add function in a separate mymath package (the first letter of the function name is capitalized):
Package mymath
Func Add (a, b int) int {return a + b}
Then we can call the Add function in the main package like this:
Package main
Import ("fmt"mymath")
Func main () {fmt.Println (mymath.Add (1,2)) / / 3}
In cross-package calls, only functions with uppercase letters can be called. This involves the visibility of packages, which will be discussed in detail later when introducing object-oriented. Here you only need to know that the uppercase Go function is equivalent to the public function in other languages. There are no keywords such as public, protected, private in Go language. It distinguishes visibility by the case of the first letter: functions with lowercase letters can only be accessed in the same package, and functions with uppercase letters can be called in other packages, as do global variables defined in the Go file.
On the basis of the Go language on the basis of the basic definition of functions and calls are shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.