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")
}
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论