diff --git a/Golang/Get Api in Golang b/Golang/Get Api in Golang new file mode 100644 index 0000000000..4fb5d553bc --- /dev/null +++ b/Golang/Get Api in Golang @@ -0,0 +1,26 @@ +package main + +import ( + // Import the gorilla/mux library we just installed + "fmt" + "net/http" + + "github.com/gorilla/mux" +) + +func main() { + // Declare a new router + r := mux.NewRouter() + + // This is where the router is useful, it allows us to declare methods that + // this path will be valid for + r.HandleFunc("/hello", handler).Methods("GET") + + // We can then pass our router (after declaring all our routes) to this method + // (where previously, we were leaving the second argument as nil) + http.ListenAndServe(":8080", r) +} + +func handler(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "Hello World!") +}