add go fiber example

This commit is contained in:
Mauricio Siu
2024-06-30 20:14:30 -06:00
parent c251b5957b
commit e613f0cae5
10 changed files with 183 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
package main
import (
"encoding/json"
"net/http"
)
// The writeJSON() method is a generic helper for writing JSON to a response
func (app *application) writeJSON(w http.ResponseWriter, sCode int, data any, headers http.Header) error {
marshalledJson, err := json.Marshal(data)
if err != nil {
return err
}
// Valid json requires newline
marshalledJson = append(marshalledJson, '\n')
for key, value := range headers {
w.Header()[key] = value
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(sCode)
w.Write(marshalledJson)
return nil
}