SilkyFalcon

Generation

generate functionMon, 05 Dec 2022

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

// Create a simple HelloWorld function that takes a name as string and returns "Hello " + name package main import ( "fmt" "net/http" "github.com/gorilla/mux" ) func main() { // Create a Router with a simple route and handler function mux := mux.NewRouter() mux.HandleFunc("/{name}", HelloWorld).Methods("GET") // Output "Hello World" when on http://localhost:8000/Hello http.ListenAndServe(":8000", mux) } func HelloWorld(w http.ResponseWriter, r *http.Request) { name := mux.Vars(r)["name"] fmt.Fprint(w, "Hello "+name) }

Questions about programming?Chat with your personal AI assistant