- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
现在我有这个:
type AppError struct{
Status int
Message string
}
func (h NearbyHandler) makeUpdate(v NearbyInjection) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
item, ok := v.Nearby[params["id"]]
if !ok {
return AppError{
500, "Missing item in map.",
}
}
}
}
问题是如果我这样做:
func (h NearbyHandler) makeUpdate(v NearbyInjection) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) AppError { // <<< return AppError
}
}
不会编译 b/c http.HandlerFunc 不会返回返回 AppError 的函数。
我的另一个问题是,如果我使用 AppError 作为返回值,如何避免显式返回 nil
?
请注意,我收到此错误:
cannot use func literal (type func(http.ResponseWriter, *http.Request) AppError) as type http.HandlerFunc in return argument
最佳答案
因此,go 的设计者没有返回请求的状态,而是给了你 ResponseWriter .这是您与客户的主要互动。例如,要设置状态代码,请执行 WriteHeader(500)
。
关于go - 从 HandlerFunc 返回错误 - 需要一个新类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53025592/
我正在尝试使用 svgo package在 svg 文件上绘制点并使用网络浏览器显示。通过查看 net/http 文档,我不知道如何将参数传递到我的 svgWeb 函数中。 下面的示例在我的网络浏览器
我已经定义了一个CustomHandler(实现ServerHTTP的结构,并且有一个返回错误的HandlerFunc) type CustomHandler struct{ HandlerFun
我正在学习来自 PHP/JS 背景的 Go。我遇到了一个我不太确定发生了什么的例子。怎么样timeHandler接收 http.ResponseWriter 和 http.Request 如果它们没有
给定: package main import ( "github.com/gin-gonic/gin" ) func main() { r := gin.Default()
我正在使用 Go 的内置 http 服务器和 pat响应某些 URL: mux.Get("/products", http.HandlerFunc(index)) func index(w http.
在链接处理程序时,该函数的返回类型为 Handler,但它实际上返回一个 HandlerFunc。这不会引发任何错误。 如何接受 HandlerFunc 代替 Handler,前者是函数类型,后者是接
现在我有这个: type AppError struct{ Status int Message string } func (h NearbyHandler) makeUpdate(v
如果我要使用 DefaultServeMux(我通过将 nil 作为第二个参数传递给 ListenAndServe 来指定),那么我可以访问 http.HandleFunc,您请参阅 Go wiki
下面的功能是如何实现的? func handle(pattern string, handler interface{}) { // ... what goes here? ... h
我正在编写一个服务来学习 Go。我的主要功能可以在下面找到。它首先读取一个 XML 文件并将它们存储在一个 slice 中。我有一个 /rss 端点,它从存储在“数据库”中的项目输出 RSS 提要。这
我将 rest API 定义为 apis.GET(/home, validatationHandler , dashboardHandler) 我想将一些数据从 validatationHan
检查以下代码时,对从函数到接口(interface)的类型转换有疑问。 代码 http_hello.go: package main import ( "fmt" "log"
所以我最近遇到了一些麻烦......这是我的代码: https://gist.github.com/anonymous/af1e6d922ce22597099521a4b2cfa16f 我的问题:我正
这是我的代码 package main import "encoding/json" import "net/http" import "time" import "fmt"
这个问题困扰我好久了。我正在使用 gin-gonic,每次我尝试使用 go run main.go 时,总是会出现此编译错误: cannot use properties.Pong (type fun
我对 this bit of code from the HTTP package 感到困惑: type HandlerFunc func(ResponseWriter, *Request) func
我看到一个 article written by Mat Ryer关于如何使用作为 func(http.ResponseWriter, *http.Request) 包装器类型的服务器类型和 http
我有这个辅助函数,可以正常编译: func Middleware(adapters ...interface{}) http.HandlerFunc { log.Info("length of
我正在寻找编写一小块限速中间件: 允许我为每个远程 IP 设置合理的速率(例如 10 个请求/秒) 可能(但不是必须)允许爆发 丢弃(关闭?)超出速率的连接并返回 HTTP 429 然后我可以将它包裹
我是一名优秀的程序员,十分优秀!