स्निपेट·मुफ़्त
Go स्निपेट
20 Go स्निपेट: HTTP हैंडलर और ServeMux, %w से एरर रैप करना, WaitGroup के साथ goroutine, टाइमआउट वाला select, टेबल-आधारित टेस्ट, JSON struct और जेनेरिक्स। कुछ के लिए Go 1.21 या 1.22 चाहिए।
मुफ़्त में इंस्टॉल करें।
अगर आपके Android डिवाइस पर PocketCode इंस्टॉल है, तो यह पेज ऐप में खुलेगा।
प्रीव्यू
20 में से 5
HTTP handlerhandler
net/http handler that sets a JSON content type
func ${1:handleHealth}(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
${2:w.WriteHeader(http.StatusOK)}
$0
}HTTP server with a muxhttpserve
ServeMux with a method pattern (Go 1.22+) and ListenAndServe
mux := http.NewServeMux()
mux.HandleFunc("${1:GET /health}", ${2:handleHealth})
log.Fatal(http.ListenAndServe("${3::8080}", mux))Wrap an errorwraperr
Return the error with context, keeping it checkable with errors.Is
if err != nil {
return fmt.Errorf("${1:load config}: %w", err)
}Check an error typeerrsas
errors.As to get a specific error type out of a chain
var ${1:pathErr} *${2:fs.PathError}
if errors.As(err, &${1:pathErr}) {
$0
}Sentinel errorerrvar
Package-level error value to compare with errors.Is
var Err${1:NotFound} = errors.New("${2:not found}")1.0.0 में बदलाव
पहला संस्करण।