Respond
Idiomatic API responses for Go.
Install / Use
/learn @matryer/RespondREADME
respond 
- v1 has been released
- Read the blog post 'API responses in Go' for an in-depth overview
Get it:
go get gopkg.in/matryer/respond.v1
Import it:
import (
"gopkg.in/matryer/respond.v1"
)
Use it:
respond.With(w, r, http.StatusOK, data)
Package respond provides low-touch API responses for Go data services.
- Idiomatic way of responding to data APIs using
respond.With - Use
respond.Withto respond with default options, or make arespond.Optionsfor advanced features - Encoder abstraction lets you easily speak different formats
BeforeandAfterfunction fields allow you to envelope and mutate data, set common HTTP headers, log activity etc.- Protected against multiple responses
Usage
The simplest use of respond is to just call respond.With inside your handlers:
func handleSomething(w http.ResponseWriter, r *http.Request) {
data, err := loadFromDB()
if err != nil {
// respond with an error
respond.With(w, r, http.StatusInternalServerError, err)
return // always return after responding
}
// respond with OK, and the data
respond.With(w, r, http.StatusOK, data)
}
To tweak the behaviour of respond.With you can wrap the handler with a respond.Options:
func main() {
opts := &respond.Options{
// options go here
}
http.Handle("/foo", opts.Handler(fooHandler))
log.Fatal(http.ListenAndServe(":8080", nil))
}
Use respond.With as normal.
For a complete list of options, see the API documentation for respond.Options.
Related Skills
node-connect
352.0kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
111.1kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
352.0kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
352.0kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
