Last active
July 6, 2021 12:53
-
-
Save badcock4412/0fba971ff11673f905cefaf94762ce96 to your computer and use it in GitHub Desktop.
Sample golang web server returning JSON objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"log" | |
"net/http" | |
"strconv" | |
) | |
func ServeJSON(rw http.ResponseWriter, obj interface{}) { | |
rw.Header().Add("content-type", "application/json") | |
rw.WriteHeader(http.StatusOK) | |
json.NewEncoder(rw).Encode(obj) | |
} | |
// plain struct | |
// remember only public fields are encoded | |
type Response1 struct { | |
StatusCode int | |
Status string | |
internalID int | |
} | |
func Serve1(rw http.ResponseWriter, req *http.Request) { | |
resp := Response1{ | |
StatusCode: 10, | |
Status: "How do you do", | |
internalID: 70, | |
} | |
ServeJSON(rw, resp) | |
} | |
// annotations | |
// json:something renames field | |
// json:,omitempty omits the field if the value is the zero value for that type (empty string, 0, etc) | |
type Response2 struct { | |
StatusCode int `json:"code"` | |
Status string `json:"description"` | |
ItemNumber int `json:"number,omitempty"` | |
ItemDescription string `json:",omitempty"` | |
} | |
func Serve2(rw http.ResponseWriter, req *http.Request) { | |
val := req.URL.Query() | |
itemNr, _ := strconv.Atoi(val.Get("item")) | |
ServeJSON(rw, Response2{ | |
StatusCode: 20, // will be 'code' when encoded | |
// Status will be zero value when ommitted but will still be in output because of annotations | |
ItemNumber: itemNr, // will be omitted by default because it is zero | |
// ItemNumber shows up at "number" if service is queried with item | |
// ItemDescription will also be zero value but will be omitted | |
}) | |
} | |
// Example of different ways to compose fields | |
type Response3 struct { | |
Status int | |
Reasons []string | |
Keys map[string]string | |
LastResult Response1 | |
NextResult interface{} | |
} | |
func Serve3(rw http.ResponseWriter, req *http.Request) { | |
ServeJSON(rw, Response3{ | |
Status: 200, | |
Reasons: []string{"bing", "bang", "boom"}, | |
Keys: map[string]string{ | |
"a": "one", | |
"b": "two", | |
}, | |
LastResult: Response1{ | |
Status: "hello", | |
StatusCode: 201, | |
}, | |
NextResult: []struct { | |
Greeting string | |
Language string | |
}{ | |
{ | |
Greeting: "hello", | |
Language: "english", | |
}, | |
{ | |
Greeting: "bonjour", | |
Language: "french", | |
}, | |
{ | |
Greeting: "TO THE MOON 🚀🚀🚀", | |
Language: "r/WallStreetBets", | |
}, | |
}, | |
}) | |
} | |
func main() { | |
http.HandleFunc("/service1", Serve1) | |
http.HandleFunc("/service2", Serve2) | |
http.HandleFunc("/service3", Serve3) | |
log.Println("serving at http://localhost:8080 . Try:") | |
log.Println("http://localhost:8080/service1") | |
log.Println("http://localhost:8080/service2") | |
log.Println("http://localhost:8080/service2?item=300") | |
log.Println("http://localhost:8080/service3") | |
panic(http.ListenAndServe(":8080", nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment