go - Golang 参数函数
问题描述
我正在尝试使用以下链接中描述的 Go 参数函数 友好 API 的功能选项- 但是我收到一个类型错误,我不知道为什么。
我在这里添加我的代码供您查看:
错误信息是:
prog.go:35:18: 在 NewServer 的参数中不能使用端口(类型 func(*Server))作为类型 func(*Server) 错误
解决方案
正如 JimB 所指出的,签名需要完全匹配,包括返回值。在您的示例代码中,要么有端口返回错误,例如:
port := func(srv *Server) error {
srv.Port = 8080
return nil
}
https://play.golang.org/p/LYb846jBD-m
或者从 NewServer 的定义中删除错误:
func NewServer(name string, options ...func(srv *Server)) *Server
推荐阅读
- node.js - Docker 容器在您的平台上为 Chrome 浏览器提供无二进制文件
- java - Android:我删除了一个包,然后我得到了 ClassNotFoundException:在路径上找不到类
- nestjs - nestjs extends jwt guard
- python - 以块的形式迭代一个 numpy 数组
- c - 无法使用中间人程序和文件描述符进行通信
- r - 使用 dplyr 改变一个新列,其值基于多个条件;尝试了 lapply 但仍然无法正常工作
- python - 比较 Pandas 中不同列对中的值
- python - Pyspark 分类数据矢量化,具有与之相关的数值
- sql - 测试 ARRAY 是否包含给定 ARRAY 的 ALL 或 NO 元素
- google-analytics - 如何更改谷歌分析中的网站网址路径?