PythonのDictionaryみたいなのがあるのを期待してたんだけどそういう便利なものはなくて構造体を使うっぽい。
package main import ( "encoding/json" "fmt" "net/http" ) type SomeStruct struct { ID int Name string } func StructHandler(w http.ResponseWriter, r *http.Request) { _struct := SomeStruct{1, "some string"} _json, err := json.Marshal(_struct) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "application/json") w.Write(_json) } func main() { http.HandleFunc("/struct.json", StructHandler) http.ListenAndServe(":15100", nil) }