package main

import "fmt"

//函数变量
func ShowBookInfoAndPrice(bookName, author string, price float64) (string, float64) {
    return bookName + author, price
}

//函数式编程
func PrintBookInfo(do func(string, string, float64) (bookInfo string, finalPrice float64),
    bookName, author string, price float64) {
    bookInfo, finalPrice := do(bookName, author, price)
    fmt.Println("bookInfo:", bookInfo)
    fmt.Println("finalPrice:", finalPrice)
}

//匿名函数
func PrintBookInfo2(bookName, author string, price float64) {
    do := func(string, string, float64) (bookInfo string, finalPrice float64) {
        return bookName + author, price
    }
    fmt.Println(do(bookName, author, price))
}

//闭包:返回值为匿名函数的函数
func PrintBookInfo3(bookName, author string, price float64) func(string, string, float64) (bookInfo string, finalPrice float64) {
    return func(string, string, float64) (bookInfo string, finalPrice float64) {
        return bookName + author, price
    }
}

//可变参数
func PrintBookNames(bookList ...string) {
    r := ""
    for _, item := range bookList {
        r += item
    }
    fmt.Println(r)
}
func main() {
    PrintBookInfo(ShowBookInfoAndPrice, "AAA", "BBB", 99.99)
    PrintBookInfo2("AAA", "BBB", 99.99)
    r := PrintBookInfo3("AAA", "BBB", 99.99)
    fmt.Println(r("AAA", "BBB", 99.99))
    PrintBookNames("AAA", "BBB", "CCC")
}

IT小马
1.2k 声望166 粉丝

Php - Go - Vue - 云原生