【go】函数类型的作用

devtools/2025/3/16 22:36:11/

Go 语言函数类型的巧妙应用

函数类型在 Go 语言中非常强大,允许将函数作为值进行传递和操作。下面详细介绍函数类型的各种妙用:

1. 回调函数

// 定义一个函数类型
type Callback func(int) int// 接受回调函数的函数
func processData(data []int, callback Callback) []int {result := make([]int, len(data))for i, v := range data {result[i] = callback(v)}return result
}// 使用示例
func main() {numbers := []int{1, 2, 3, 4, 5}// 使用匿名函数作为回调doubled := processData(numbers, func(x int) int {return x * 2})// 使用已定义函数作为回调squared := processData(numbers, square)fmt.Println(doubled)  // [2 4 6 8 10]fmt.Println(squared)  // [1 4 9 16 25]
}func square(x int) int {return x * x
}

2. 策略模式实现

type PaymentStrategy func(amount float64) boolfunc processPayment(amount float64, strategy PaymentStrategy) bool {return strategy(amount)
}// 各种支付策略
func creditCardPayment(amount float64) bool {// 信用卡支付逻辑return true
}func alipayPayment(amount float64) bool {// 支付宝支付逻辑return true
}// 使用示例
func pay(amount float64, paymentMethod string) bool {switch paymentMethod {case "credit":return processPayment(amount, creditCardPayment)case "alipay":return processPayment(amount, alipayPayment)default:return false}
}

3. 装饰器模式

type HttpHandler func(w http.ResponseWriter, r *http.Request)// 日志装饰器
func LoggingDecorator(handler HttpHandler) HttpHandler {return func(w http.ResponseWriter, r *http.Request) {fmt.Printf("Request: %s %s\n", r.Method, r.URL.Path)handler(w, r)fmt.Println("Request completed")}
}// 认证装饰器
func AuthDecorator(handler HttpHandler) HttpHandler {return func(w http.ResponseWriter, r *http.Request) {// 检查认证信息if authenticate(r) {handler(w, r)} else {http.Error(w, "Unauthorized", http.StatusUnauthorized)}}
}// 使用装饰器
func main() {http.HandleFunc("/api/data", LoggingDecorator(AuthDecorator(handleData)))http.ListenAndServe(":8080", nil)
}func handleData(w http.ResponseWriter, r *http.Request) {// 业务逻辑
}

4. 函数选项模式

type Server struct {host stringport inttimeout time.DurationmaxConn int
}type ServerOption func(*Server)// 创建Server的选项函数
func WithHost(host string) ServerOption {return func(s *Server) {s.host = host}
}func WithPort(port int) ServerOption {return func(s *Server) {s.port = port}
}func WithTimeout(timeout time.Duration) ServerOption {return func(s *Server) {s.timeout = timeout}
}func WithMaxConn(maxConn int) ServerOption {return func(s *Server) {s.maxConn = maxConn}
}// 创建服务器
func NewServer(options ...ServerOption) *Server {// 设置默认值server := &Server{host:    "localhost",port:    8080,timeout: 30 * time.Second,maxConn: 100,}// 应用所有选项for _, option := range options {option(server)}return server
}// 使用示例
func main() {server := NewServer(WithHost("example.com"),WithPort(9000),WithTimeout(60 * time.Second),)// 使用server...
}

5. 中间件链

type Middleware func(http.Handler) http.Handler// 中间件链
func Chain(middlewares ...Middleware) Middleware {return func(next http.Handler) http.Handler {for i := len(middlewares) - 1; i >= 0; i-- {next = middlewares[i](next)}return next}
}// 使用示例
func main() {handler := http.HandlerFunc(finalHandler)// 创建中间件链chain := Chain(loggingMiddleware,authMiddleware,rateLimitMiddleware,)// 应用中间件链http.Handle("/api", chain(handler))http.ListenAndServe(":8080", nil)
}

6. 延迟执行与钩子函数

type ShutdownHook func()type App struct {shutdownHooks []ShutdownHook
}func (a *App) AddShutdownHook(hook ShutdownHook) {a.shutdownHooks = append(a.shutdownHooks, hook)
}func (a *App) Shutdown() {// 按照注册顺序的相反顺序执行钩子for i := len(a.shutdownHooks) - 1; i >= 0; i-- {a.shutdownHooks[i]()}
}// 使用示例
func main() {app := &App{}// 注册数据库关闭钩子app.AddShutdownHook(func() {fmt.Println("关闭数据库连接")})// 注册文件清理钩子app.AddShutdownHook(func() {fmt.Println("清理临时文件")})// 应用运行...// 关闭应用app.Shutdown()
}

7. 操作集合的函数

type FilterFunc func(int) bool
type MapFunc func(int) int
type ReduceFunc func(int, int) int// 过滤集合
func Filter(nums []int, filter FilterFunc) []int {result := []int{}for _, n := range nums {if filter(n) {result = append(result, n)}}return result
}// 映射集合
func Map(nums []int, mapper MapFunc) []int {result := make([]int, len(nums))for i, n := range nums {result[i] = mapper(n)}return result
}// 归约集合
func Reduce(nums []int, initialValue int, reducer ReduceFunc) int {result := initialValuefor _, n := range nums {result = reducer(result, n)}return result
}// 使用示例
func main() {numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}// 过滤偶数evens := Filter(numbers, func(n int) bool {return n%2 == 0})// 将数字翻倍doubled := Map(evens, func(n int) int {return n * 2})// 求和sum := Reduce(doubled, 0, func(acc, n int) int {return acc + n})fmt.Println("结果:", sum) // 60
}

8. 依赖注入

type UserRepository interface {FindByID(id int) (User, error)
}type UserService struct {repo UserRepository
}func NewUserService(repo UserRepository) *UserService {return &UserService{repo: repo}
}// 测试时可以轻松注入模拟实现
func TestUserService(t *testing.T) {mockRepo := &MockUserRepository{FindByIDFunc: func(id int) (User, error) {return User{ID: id, Name: "测试用户"}, nil},}service := NewUserService(mockRepo)// 测试 service...
}

9. 自定义排序

type Person struct {Name stringAge  int
}type SortBy func(p1, p2 *Person) booltype PersonSorter struct {people []Personless   SortBy
}func (s PersonSorter) Len() int           { return len(s.people) }
func (s PersonSorter) Swap(i, j int)      { s.people[i], s.people[j] = s.people[j], s.people[i] }
func (s PersonSorter) Less(i, j int) bool { return s.less(&s.people[i], &s.people[j]) }// 使用示例
func main() {people := []Person{{"张三", 30},{"李四", 25},{"王五", 35},}// 按年龄排序sort.Sort(PersonSorter{people: people,less: func(p1, p2 *Person) bool {return p1.Age < p2.Age},})fmt.Println("按年龄排序:", people)// 按姓名排序sort.Sort(PersonSorter{people: people,less: func(p1, p2 *Person) bool {return p1.Name < p2.Name},})fmt.Println("按姓名排序:", people)
}

10. 惰性计算

type LazyEval func() interface{}func computeExpensiveValue() LazyEval {computed := falsevar result interface{}return func() interface{} {if !computed {fmt.Println("执行昂贵计算...")// 模拟耗时操作time.Sleep(1 * time.Second)result = 42computed = true}return result}
}// 使用示例
func main() {// 创建惰性计算lazy := computeExpensiveValue()fmt.Println("惰性计算创建后,尚未执行计算")// 调用时才执行实际计算value := lazy()fmt.Println("第一次获取值:", value)// 再次调用不会重复计算value = lazy()fmt.Println("第二次获取值:", value)
}

函数类型使 Go 拥有了函数式编程的部分能力,同时保持了语言的简洁性和性能,这使得它在构建灵活、可测试和可维护的代码时非常有价值。


http://www.ppmy.cn/devtools/167662.html

相关文章

算力服务器主要是指什么?

随着科技的快速发展&#xff0c;人工智能也逐渐兴起&#xff0c;算力服务器也受到了各个企业的重视&#xff0c;本文就来为大家介绍一下算力服务器主要都是指什么吧&#xff01; 算力服务器对于人工智能领域来说&#xff0c;在深度学习模型的训练和推理过程中扮演着非常重要的角…

ngx_conf_read_token

Ubuntu 下 nginx-1.24.0 源码分析 - ngx_conf_read_token-CSDN博客 static ngx_int_t ngx_conf_read_token(ngx_conf_t *cf) {u_char *start, ch, *src, *dst;off_t file_size;size_t len;ssize_t n, size;ngx_uint_t found, need_space, last_space…

时序约束整理

输入输出约束 FPGA整体概念 (1) Tdin为从FPGA的IO口到FPGA内部寄存器输入端的延时; (2) Tclk为从FPGA的IO口到FPGA内部寄存器时钟端的延时; (3) Tus/Th为FPGA内部寄存器的建立时间和保持时间; (4) Tco为FPGA内部寄存器传输时间; (5) Tout为从FPGA寄存器输出…

Python 实现的采集诸葛灵签

Python 实现的采集诸葛灵签 项目介绍 这是一个基于 Python 开发的诸葛灵签数据采集和展示项目。通过爬虫技术获取诸葛神签的签文和解签内容&#xff0c;并提供数据存储和查询功能。 项目结构 zhuge/├── zhuge_scraper.py # 爬虫主程序├── zhuge_pages/ # 数据存储目录…

Linux练级宝典->多线程

目录 Linux线程概念 什么是线程 二级页表 线程的优点 线程的缺点 线程异常 线程用途 Linux进程和线程 线程共享资源 线程和进程的关系图 Linux线程控制 POSIX线程库 线程创建 线程等待 线程终止 线程分离 Linux线程概念 什么是线程 在一个程序里的一个执行流叫做…

什么是 slot-scope?怎么理解。

1. 什么是 slot-scope&#xff1f; slot-scope 是 Vue 2 中用于作用域插槽的语法。它的作用是让子组件可以把一些数据传递给父组件&#xff0c;父组件可以根据这些数据自定义渲染内容。 简单来说&#xff1a; 子组件&#xff1a;负责提供数据。 父组件&#xff1a;负责根据数…

专访数势科技谭李:智能分析 Agent 打通数据平权的最后一公里

作者|斗斗 编辑|皮爷 出品|产业家 伦敦塔桥下的泰晤士河底&#xff0c;埋藏着工业革命的隐秘图腾——布鲁内尔设计的隧道盾构机。在19世纪城市地下轨道建设的过程中&#xff0c;这个直径11米的钢铁巨兽没有选择拓宽河道&#xff0c;而是开创了地下通行的新维度。 “我们不…

KVM安全模块生产环境配置与优化指南

KVM安全模块生产环境配置与优化指南 一、引言 在当今复杂多变的网络安全环境下&#xff0c;生产环境中KVM&#xff08;Kernel-based Virtual Machine&#xff09;的安全配置显得尤为重要。本指南旨在详细阐述KVM安全模块的配置方法&#xff0c;结合强制访问控制&#xff08;M…