SoFunction
Updated on 2025-03-05

Functional programming practice in Go language

This article mainly explains the concepts and uses of functional programming in Go language, and shares them with you, as follows:

Main knowledge points:

  1. Go's support for functional programming is mainly reflected in closures
  2. A closure is a function that can read variables inside other functions. Only subfunctions inside functions can read local variables, so closures can be understood as "functions defined inside a function". In essence, closures are bridges connecting the inside and outside of the function.
  3. Basic use of learning closures
  4. Standard closures are immutable: they cannot have states, they can only have constants and functions, and functions can only have one parameter, but they generally do not need to be strictly followed.
  5. Implement Fibonacci sequences using closures
  6. Learn to understand functions to implement interfaces
  7. Use functions to traverse binary tree

Specific code examples are as follows:

package main

import (
 "fmt"
 "io"
 "strings"
 "bufio"
)

//Ordinary closurefunc adder() func(int) int {
 sum := 0
 return func(v int) int {
 sum += v
 return sum
 }
}

//Stateless closure without variablestype iAdder func(int) (int, iAdder)
func adder2(base int) iAdder {
 return func(v int) (int, iAdder) {
 return base + v, adder2(base + v)
 }
}

//Implement Fibonacci sequences using closuresfunc Fibonacci() func() int {
 a, b := 0, 1
 return func() int {
 a, b = b, a+b
 return a
 }
}

//Implement the interface for a function, read the above method as a filetype intGen func() int
//Implement interfaces for all functions of this type abovefunc (g intGen) Read(
 p []byte) (n int, err error) {
 next := g()
 if next > 10000 {
 return 0, 
 }
 s := ("%d\n", next)

 // TODO: incorrect if p is too small!
 return (s).Read(p)
}
//Read the file through Readerfunc printFileContents(reader ) {
 scanner := (reader)
 for () {
 (())
 }
}

func main() {
 //Normal closure call a := adder()
 for i := 0; i < 10; i++ {
 var s int =a(i)
 ("0 +...+ %d = %d\n",i, s)
 }
 //Status closure without variables Call b := adder2(0)
 for i := 0; i < 10; i++ {
 var s int
 s, b = b(i)
 ("0 +...+ %d = %d\n",i, s)
 }

 //Call Fibonacci sequence Generate fib:=Fibonacci()
 (fib(),fib(),fib(),fib(),fib(),fib(),fib(),fib())


 var f intGen = Fibonacci()
 printFileContents(f)
}

The following code demonstrates the function traversing the binary tree:

package main
import "fmt"
type Node struct {
 Value  int
 Left, Right *Node
}

func (node Node) Print() {
 (, " ")
}

func (node *Node) SetValue(value int) {
 if node == nil {
 ("Setting Value to nil " +
 "node. Ignored.")
 return
 }
  = value
}

func CreateNode(value int) *Node {
 return &Node{Value: value}
}

//Provide implementation for TraverseFunc methodfunc (node *Node) Traverse() {
 (func(n *Node) {
 ()
 })
 ()
}
//Add a method to the Node structure TraverseFunc,//This method passes in a method parameter, which is executed during traversal.func (node *Node) TraverseFunc(f func(*Node)) {
 if node == nil {
 return
 }
 (f)
 f(node)
 (f)
}


func main() {
 var root Node
 root = Node{Value: 3}
  = &Node{}
  = &Node{5, nil, nil}
  = new(Node)
  = CreateNode(2)
 (4)
 () // Print packaged
 //The following is a custom implementation through anonymous functions nodeCount := 0
 (func(node *Node) {
 nodeCount++
 })
 ("Node count:", nodeCount) //Node count: 5
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.