SilkyFalcon

Generation

generate functionMon, 05 Dec 2022

Create a webserver function with arguments that are parsed to json format

package main import ( "fmt" "net/http" "encoding/json" ) type Payload struct { Stuff Data } type Data struct { Fruit Fruits Veggies Vegetables } type Fruits map[string]int type Vegetables map[string]int func ServeHTTP(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") p := Payload {Data: Data{Fruit: Fruits{"Apple": 5, "Orange": 6}, Veggies: Vegetables{"Carrats": 7, "Peppers": 8}}} enc := json.NewEncoder(w) enc.Encode(p) } func main() { fmt.Println("Starting HTTP server...") http.HandleFunc("/", ServeHTTP) http.ListenAndServe(":8080", nil) }

Questions about programming?Chat with your personal AI assistant