This is a creation in Article, where the information may have evolved or changed. 2012-05-19 translated from Here, the original text has been expanded, but also some limitations.
is go a functional programming language ? No, of course not.
So, does go provide a function ? Yes, of course, most programming languages provide functions, and go is no exception. Don't you believe it? I'll use the code to shut you up:
func SayHello () { fmt. Println ("Hello")}
See it. Go uses the keyword func to define the function and write the function logic in the body of the function.
can the Go function accept parameters ? Well, I see another idiot's problem, hehe. Oh, I understand, maybe my sayhello function gives you the illusion that I will transform my code:
string ) { fmt. Println ("" ". " )}
The function Sayhellotosomeone accepts a string parameter of type name.
can the GO function have a return value? Yes, yes, yes ! Just like a mathematical function, the Go function can return something to the caller. To demonstrate this, I'll rewrite a function:
func getgreeting (name string ) string {greeting: = Span style= "color: #800000;" > " hello " + name + " " return greeting} // Test Greeting: = getgreeting (" bob ) fmt. Println (greeting) // outputs "Hello Bob."
Of course there's nothing else that can be done in other languages. But are you ready to take a surprise?
The return value of the Go function is somewhat different from other Class C languages, for example, you can specify a name for the return value. This brings at least 2 benefits: 1. You do not need to define a variable in the function body for the return value. 2. You do not need to add the return value after the return statement. Go will automatically add the return value.
string string ) { """. " return }
The getgreeting function specifies a name for its return value: greeting. The greeting variable can be used directly in the function body, and greeting is actually equivalent to a local variable defined in the function. And as you can see, the return statement of the getgreeting function is not followed by the returned value, because go automatically returns the value of the greeting variable to the caller.
can the GO function return multiple values ? this, this, are you whimsical? But the powerful go function can do this, haha. To know that a function that can have multiple return values avoids a lot of ugly code, here's an example:
Type Stackstruct{posintdata [Ten]int}func (S*stack) Pop () (ValueintOkBOOL) { ifS.pos >0{S.pos--OK=truevalue=S.data[s.pos]return} OK=false return}
The stack type is defined first in the code and the POP function is provided. The POP function returns 2 values: Value and OK, each representing the value of the pop and whether the pop operation succeeded.
can the Go function accept a function as an argument ? Well, I think you're finally starting to focus. If you were a doctor, would you be bored with the need to sayhello your patients every time? It doesn't matter, go can help you. First you need to define a new data structure:
struct { patients []string}// The []string (string array) is wrapped into a pointer of type tormentlist Func newtormentlist (People []string) *tormentlist { return &tormentlist{ People}}
Next, let's quietly add the map method to the Tormentlist type:
Func (g *tormentlist) Map (f func (string)) { // traversal g.patients, call F method for each of its value for _, val: = Range (g.patients) { f (val) }}
The map method accepts the F function as its input parameter, and the F function accepts a value of type string. Traverse the Tormentlist patient and call the F function for each patient.
now that everything is ready, the rest is the test:
Patients: = []string{"Anand","David","Ivan","JoJo","Jin","Mon","Peter","Sachin"}GL:=newtormentlist (patients)//Remember the Sayhellotosomeone function you defined above?GL. Map (Sayhellotosomeone)/*outputs the Following:hello anand.hello david.hello ivan.hello jojo.hello jin.hello Mon.hello Peter.Hello Sachin.
*/
can the return value of the Go function be a function ? let's consider a realistic question: if you have a list of people who have eaten sushi, can you determine whether he is on the list according to the name of the person? This is a very simple question, you just have to go through the list. Well, if you have a weak foundation, you don't know how to traverse the list. I will provide you with a brush selector:
Func screen (patients []string) Func (string)BOOL { //defines an anonymous function and returns returnFunc (namestring)BOOL { for_, Soul: =Range Patients {ifSoul = =Name {return true } } return false }} The screen method returns the selected function to the caller so that you don't have to know how to traverse the list, just call the function that I returned to you to:
//A list of people who have eaten sushi .Those_who_bought_sushi: = []string{"Anand","JoJo","Jin","Mon","Peter","Sachin"}//get the brush selector functionBought_sushi: =Screen (Those_who_bought_sushi)//Call the brush selector function to know if someone is on the listFmt. Println (Bought_sushi ("Anand"))//trueFmt. Println (Bought_sushi ("Alex"))//false