code update recorded at: 23/03/26 10:04:43
This commit is contained in:
commit
922c187696
|
|
@ -0,0 +1,32 @@
|
|||
package main
|
||||
|
||||
// Import necessary packages
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Define the handler function
|
||||
func handler(req http.ResponseWriter, res *http.Request) {
|
||||
// Initialize a map to hold the response body
|
||||
response := map[string]string{
|
||||
"message": "Hello, World!", // Set the message to "Hello, World!"
|
||||
}
|
||||
|
||||
// Set the Content-Type header to application/json
|
||||
req.Header().Set("Content-Type", "application/json")
|
||||
|
||||
// Encode the response map into JSON and write it to the response
|
||||
json.NewEncoder(req).Encode(response)
|
||||
|
||||
// Marshal the response map into JSON
|
||||
responseJson, err := json.Marshal(response)
|
||||
if err != nil {
|
||||
http.Error(req, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Print the JSON response to stdout
|
||||
fmt.Println(string(responseJson))
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user