In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "what is the concept of functions and pointers in Go language". The editor shows you the operation process through practical cases. The operation method is simple, fast and practical. I hope this article "what is the concept of functions and pointers in Go language" can help you solve the problem.
Function
A function is a basic block of code that is generally used to reuse code that needs to be executed repeatedly. In go, a function is a "first-class citizen", which is similar to js, that is, it can be passed as a variable.
Function declaration
Because it is a strongly typed language, unlike js, you need to specify the type of parameter and return value in the process of function declaration.
Func max (N1, N2 int) int {var result int if N1 > = N2 {result = N1} if N1
< n2 { result = n2 } return result }When declaring function parameters and types, similar to declaring variables, you can specify the types of multiple parameters at once, or you can specify multiple parameters as different types.
Func max (N1 int, N2 int) int {. }
If the function returns more than one value, you need to specify the type of each return value when you specify the return type.
Func max (N1 int, N2 int) (error, int) {. Return errors.New (""), result}
The above code indicates that two values need to be returned when returned. The first value is the error object, which is used to indicate whether an exception occurred during execution. This is also a common way of writing error-first callback in Node.js.
Special function
In go, there are two special functions: main and init. After these two functions are declared, they generally do not need to be called actively, and there is an automatic execution mechanism.
Func main ()
The main function is the default entry function in the go language and can only be applied in package main, if it is not executed in other package. The main function has the following points to note:
Parameters cannot be defined
Cannot define return value
Must be declared in package main
Func init ()
All packages of the init function are executed when they are started, earlier than the main function, and like the main function, parameters and return values cannot be defined.
Package main import "fmt" func init () {fmt.Println ("execute init function\ n")} func main () {fmt.Println ("execute main function\ n")}
Function call
The call of the function is relatively simple, similar to other programming languages, only the parameters that the function needs to accept are passed into it, and the corresponding return value can be obtained at the end of the execution.
/ / define the max function func max (N1, N2 int) int {var result int if N1 > = N2 {result = N1} if N1
< n2 { result = n2 } return result } func main () { var result = max(5, 100) fmt.Println("max return", result) } 匿名函数 匿名函数就是一个没有定义函数名的函数,匿名函数可以当成一个值,将其赋值放到某个变量中。这也是之前为什么说函数是『一等公民』,就是可以将函数当成一个变量。 var max = func (n1, n2 int) int { var result int if n1 >= N2 {result = N1} if N1
< n2 { result = n2 } return result } var result = max(5, 100) fmt.Println("max return", result)立即执行函数 由于 go 中的函数是 『一等公民』,可以在声明之后立即执行,就是在函数声明结束后,直接加上一个括号,表示该函数会立即执行,执行之后的结果可以通过变量进行接收。 import "math" var Pi = func () float64 { return math.Pi }() fmt.Println("PI =",Pi) 闭包 "闭包就是能够读取其他函数内部变量的函数。在本质上,闭包是将函数内部和函数外部连接起来的桥梁。 ——百度百科 上面的描述来自百度百科,初次看概念比较难理解,如果站在使用的角度来说,闭包就是在一个函数调用后,返回另一个匿名函数,并保持当前函数内的局部变量,可以给匿名函数引用。 下面我们可以简单实现一个迭代器函数,该函数接受一个切片,返回一个匿名函数,该匿名函数每次执行,都会取出切片的一个值,直到全部读取。 func generate(slice []int) func() (bool, int) { i := 0 length := len(slice) return func () (bool, int) { if i >= length {return true, 0} var result = slice [I] ionization + return false, result}} func main () {slice: = [] int {1,2,3,4,5} nextNum: = generate (slice) done, result: = nextNum () / / until done is not equal to false, for done = = false {fmt.Println (result, done) done, result = nextNum ()} fmt.Println (result, done)}
Pointer
The variable we often mentioned earlier generally refers to a value, and the pointer points to the location where the variable is stored in memory. Pointers can also be stored in a variable called a pointer variable.
Pointer variable declaration
When declaring a pointer variable, you need to point to which type of pointer, because different types of values take up different amounts of space in memory, just knowing the memory address is not enough, and you also need to know how much space the variable takes up in memory. To declare a pointer variable, you only need to add * before the type.
Var point * int / / declares pointer variable assignment of type int
To assign a value to a pointer variable, you need to precede the variable of the corresponding type with the & symbol to indicate the address of the variable to be fetched.
Var I = 1 var point * int point = & I value transfer and reference transfer
In general, the parameter we pass into the function is only the value of the variable, which is called value transfer, and the modification of the parameter within the function will not affect the external variable.
Func addOne (slice [] int, number int) {slice = append (slice, number) fmt.Println ("inner slice =", slice)} slice: = [] int {1,2,3} addOne (slice, 100) fmt.Println ("outer slice =", slice)
In the above code, we write a function that appends a value to the incoming slice, and after calling it, we will find that there is no variable in the value of the external slice.
If you need the value of an external variable to change with the function call, you need to pass the pointer of the variable to the function, which is called reference passing. In this way, modifying the parameters in the function will affect the external variables.
/ / slice is the pointer variable func addOne (slice * [] int, number int) {/ / the value corresponding to the slice pointer can be fetched through * slice * slice = append (* slice, number) fmt.Println ("inner slice =", * slice)} slice: = [] int {1,2,3} addOne (& slice, 100) fmt.Println ("outer slice =", slice)
This is the end of the introduction to "what is the concept of functions and pointers in Go". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.