How Golang uses anonymous functions
This article will explain in detail how Golang uses anonymous functions. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Two ways of using anonymous functions
First, you can use it directly when defining anonymous functions (this method can only be used once)
Package mainimport ("fmt") func main () {res1: = func (N1 int, N2 int) int {return N1 + N2} (10Power30) / / 10 fmt.Println 30 in parentheses is equivalent to a parameter list, corresponding to N1 and N2 fmt.Println ("res1=", res1)} D:\ goproject\ src\ main > go run hello.gores1= 40, respectively.
Second, assign an anonymous function to a variable (function variable), and then call the anonymous function through that variable.
Package mainimport ("fmt") func main () {/ / assign the anonymous function fun to the variable test_fun / / then the data type of test _ fun is the function type You can call test_fun: = func (N1 int, N2 int) int {return N1-N2} res2: = test_fun (10,30) res3: = test_fun (50,30) fmt.Println ("res2=", res2) fmt.Println ("res3=", res3) fmt.Printf ("% T", test_fun)} D:\ goproject\ src\ main > go run hello.gores2=-20res3 = 20func (int) via test_fun Int) int's article on "how Golang uses anonymous functions" ends here. Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.