{"id":587,"date":"2023-08-07T13:38:56","date_gmt":"2023-08-07T13:38:56","guid":{"rendered":"http:\/\/192.168.0.142\/?p=587"},"modified":"2023-08-09T13:37:27","modified_gmt":"2023-08-09T13:37:27","slug":"rest-api-in-go-using-gorilla-mux","status":"publish","type":"post","link":"http:\/\/192.168.0.142\/rest-api-in-go-using-gorilla-mux\/","title":{"rendered":"REST Api in Go using Gorilla\/Mux"},"content":{"rendered":"\n

This post explains how to develop REST APIs in Go language using Gorilla Mux<\/strong><\/p>\n\n\n\n

Gorilla Mux is a package from the Gorilla Web Toolkit. It provides simple ways to create HTTP routers. Using the package, we can redirect program flow to respective HTTP handler functions.<\/strong><\/p>\n\n\n\n

Create a directory for the project<\/strong><\/p>\n\n\n\n

mkdir api-demo<\/code><\/pre>\n\n\n\n

Initialize a go project named ‘api-demo’<\/strong><\/p>\n\n\n\n

go mod init rinoymjoseph.github.com\/api-demo<\/code><\/pre>\n\n\n\n

Install Gorilla Mux<\/strong><\/p>\n\n\n\n

go get -u github.com\/gorilla\/mux<\/code><\/pre>\n\n\n\n

Add a file named warrior.go which contains a type<\/strong><\/p>\n\n\n\n

package main\n\ntype Warrior struct {\n\tId   int    `json:\"id\"`\n\tName string `json:\"name\"`\n}\n<\/code><\/pre>\n\n\n\n

Add a file named handlers.go which contains handler functions<\/strong><\/p>\n\n\n\n

package main\n\nimport (\n\t\"encoding\/json\"\n\t\"fmt\"\n\t\"log\"\n\t\"net\/http\"\n)\n\nfunc health(w http.ResponseWriter, r *http.Request) {\n\tlog.Println(\"Entering health check\")\n\tw.WriteHeader(http.StatusOK)\n\tfmt.Fprintf(w, \"API is up and running\")\n}\n\nfunc getWarriors(w http.ResponseWriter, r *http.Request) {\n\tlog.Println(\"Entering getWarriors\")\n\tpersons := buildWarriors()\n\tw.Header().Set(\"Content-Type\", \"application\/json\")\n\tw.WriteHeader(http.StatusOK)\n\tjsonResponse, err := json.Marshal(persons)\n\tif err != nil {\n\t\treturn\n\t}\n\tw.Write(jsonResponse)\n}\n\nfunc buildWarriors() []Warrior {\n\tvar warriors []Warrior\n\n\tvar warrior Warrior\n\twarrior.Id = 1\n\twarrior.Name = \"Link\"\n\twarriors = append(warriors, warrior)\n\n\twarrior.Id = 2\n\twarrior.Name = \"Zelda\"\n\twarriors = append(warriors, warrior)\n\n\twarrior.Id = 3\n\twarrior.Name = \"Revali\"\n\twarriors = append(warriors, warrior)\n\treturn warriors\n}\n<\/code><\/pre>\n\n\n\n

Create a main.go file and add below contents<\/strong><\/p>\n\n\n\n

package main\n\nimport (\n\t\"log\"\n\t\"net\/http\"\n\n\t\"github.com\/gorilla\/mux\"\n)\n\nfunc main() {\n\tlog.Println(\"Starting API server\")\n\t\/\/create a new router\n\trouter := mux.NewRouter()\n\tlog.Println(\"Creating routes\")\n\t\/\/specify endpoints\n\trouter.HandleFunc(\"\/health\", health).Methods(\"GET\")\n\trouter.HandleFunc(\"\/warriors\", getWarriors).Methods(\"GET\")\n\thttp.Handle(\"\/\", router)\n\t\/\/start and listen to requests\n\thttp.ListenAndServe(\":8080\", router)\n}\n<\/code><\/pre>\n\n\n\n

Get the packages<\/strong><\/p>\n\n\n\n

go get .\ngo mod tidy<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"

This post explains how to develop REST APIs in Go language using Gorilla Mux Gorilla Mux is a package from the Gorilla Web Toolkit. It provides simple ways to create HTTP routers. Using the package, we can redirect program flow to respective HTTP handler functions. Create a directory for the project Initialize a go project … Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":721,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[56],"tags":[57,61],"_links":{"self":[{"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/posts\/587"}],"collection":[{"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/comments?post=587"}],"version-history":[{"count":8,"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/posts\/587\/revisions"}],"predecessor-version":[{"id":663,"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/posts\/587\/revisions\/663"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/media\/721"}],"wp:attachment":[{"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/media?parent=587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/categories?post=587"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/tags?post=587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}