今天就跟大家聊聊有关怎么在Golang中实现函数式编程,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
主要知识点:
Go语言对函数式编程的支持主要体现在闭包上面
闭包就是能够读取其他函数内部变量的函数。只有函数内部的子函数才能读取局部变量,所以闭包可以理解成“定义在一个函数内部的函数“。在本质上,闭包是将函数内部和函数外部连接起来的桥梁。
学习闭包的基本使用
标准的闭包具有不可变性:不能有状态,只能有常量和函数,而且函数只能有一个参数,但是一般可以不用严格遵守
使用闭包 实现 斐波那契数列
学习理解函数实现接口
使用函数遍历二叉树
具体代码示例如下:
package main
import (
"fmt"
"io"
"strings"
"bufio"
)
//普通闭包
func adder() func(int) int {
sum := 0
return func(v int) int {
sum += v
return sum
}
}
//无状态 无变量的闭包
type iAdder func(int) (int, iAdder)
func adder2(base int) iAdder {
return func(v int) (int, iAdder) {
return base + v, adder2(base + v)
}
}
//使用闭包实现 斐波那契数列
func Fibonacci() func() int {
a, b := 0, 1
return func() int {
a, b = b, a+b
return a
}
}
//为函数 实现 接口,将上面的方法 当作一个文件进行读取
type intGen func() int
//为所有上面这种类型的函数 实现接口
func (g intGen) Read(
p []byte) (n int, err error) {
next := g()
if next > 10000 {
return 0, io.EOF
}
s := fmt.Sprintf("%d\n", next)
// TODO: incorrect if p is too small!
return strings.NewReader(s).Read(p)
}
//通过 Reader读取文件
func printFileContents(reader io.Reader) {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
}
func main() {
//普通闭包调用
a := adder()
for i := 0; i < 10; i++ {
var s int =a(i)
fmt.Printf("0 +...+ %d = %d\n",i, s)
}
//状态 无变量的闭包 调用
b := adder2(0)
for i := 0; i < 10; i++ {
var s int
s, b = b(i)
fmt.Printf("0 +...+ %d = %d\n",i, s)
}
//调用 斐波那契数列 生成
fib:=Fibonacci()
fmt.Println(fib(),fib(),fib(),fib(),fib(),fib(),fib(),fib())
var f intGen = Fibonacci()
printFileContents(f)
}
以下代码演示函数遍历二叉树:
package main
import "fmt"
type Node struct {
Value int
Left, Right *Node
}
func (node Node) Print() {
fmt.Print(node.Value, " ")
}
func (node *Node) SetValue(value int) {
if node == nil {
fmt.Println("Setting Value to nil " +
"node. Ignored.")
return
}
node.Value = value
}
func CreateNode(value int) *Node {
return &Node{Value: value}
}
//为 TraverseFunc 方法提供 实现
func (node *Node) Traverse() {
node.TraverseFunc(func(n *Node) {
n.Print()
})
fmt.Println()
}
//为 Node 结构增加一个方法 TraverseFunc ,
//此方法 传入一个方法参数,在遍历是执行
func (node *Node) TraverseFunc(f func(*Node)) {
if node == nil {
return
}
node.Left.TraverseFunc(f)
f(node)
node.Right.TraverseFunc(f)
}
func main() {
var root Node
root = Node{Value: 3}
root.Left = &Node{}
root.Right = &Node{5, nil, nil}
root.Right.Left = new(Node)
root.Left.Right = CreateNode(2)
root.Right.Left.SetValue(4)
root.Traverse() // 进行了 打印封装
//以下通过匿名函数,实现了 自定义实现
nodeCount := 0
root.TraverseFunc(func(node *Node) {
nodeCount++
})
fmt.Println("Node count:", nodeCount) //Node count: 5
}
golang可以做服务器端开发,但golang很适合做日志处理、数据打包、虚拟机处理、数据库代理等工作。在网络编程方面,它还广泛应用于web应用、API应用等领域。
看完上述内容,你们对怎么在Golang中实现函数式编程有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。