Is go a functional programming language?

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

[2012-05-19 translation from here , the original text has been expanded, but also some limitations. Copyright belongs to the original author, reproduced must retain this statement.


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.
Func SayHello () {    fmt. Println ("Hello")}
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, the question ... Oh, I get it, maybe my sayhello function gives you the illusion that I'm going to remodel my code:
Func sayhellotosomeone (name string) {    FMT. Println ("Hello" + name + ".")}  
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: = "Hello" + name + "."   return greeting}//testgreeting: = 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 a minimum of 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 a return value after the return statement. Go will automatically add the return value.
Func getgreeting (name string) (greeting string) {   greeting = "Hello" + name + "."   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 Stack struct {    pos  int    data [10]int}func (S *stack) Pop () (value int, OK bool) {    If S.pos > 0 {
    s.pos--        OK = true        value = 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. You first need to define a new data structure:
Type tormentlist struct {    patients []string}//] Wraps []string (string array) into the Tormentlist type pointer 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 patient of tormentlist and call the F function for each patient. Now that everything is ready, the rest is testing:
Patients: = []string{"Anand", "David", "Ivan", "JoJo", "Jin", "Mon", "Peter", "sachin"}GL: = newtormentlist (patients)//Remember The Sayhellotosomeone function 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, what if you're weak in go and don't know how to traverse the list? It doesn't matter, I will provide you with a brush selector:
Func screen (patients []string) func (string) bool {    //defines an anonymous function and returns return   func (name string) bool {       for _, Soul: = Range patients {           if soul = = 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:
The list of people who have eaten sushi those_who_bought_sushi: = []string{"Anand", "JoJo", "Jin", "Mon", "Peter", "Sachin"}//get the brush selector function Bought_sushi: = Screen (Those_who_bought_sushi)//Call the brush selector function to know if someone is fmt on the list. Println (Bought_sushi ("Anand"))//TRUEFMT. Println (Bought_sushi ("Alex"))//False
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.