DEV Community

Build Your First Rest API with GO

Mofi Rahman on September 04, 2019

Build Your First Rest API with GO There is three part to this workshop. API Rest API Rest API with GO API If you have bee...
Collapse
 
aut0poietic profile image
Jer

Hey @moficodes ! I wanted to thank you for the article. It works as a great bridge between some of the more terse tutorials and examples out there and winding up with a working SPA+API running on Go.

So, thanks for the work you put in ! I greatly appreciate it.

Collapse
 
moficodes profile image
Mofi Rahman

I am glad you liked it. 🥰

Collapse
 
ryanmccormick profile image
Ryan McCormick

Great primer for writing REST APIs in golang. I am a go newbie and stumbled on to this article. Quick note about some of the example code for typo fix or for anyone else working through the example:

The beginning of the exercise has the w.WriteHeader and w.Header().Set lines flip-flopped.
while running the example at the beginning I kept getting "text/plain" for my response content type instead of "application/json". It looks to be correct elsewhere but w.Header().Set should be called before w.WriteHeader. According to docs: "Changing the header map after a call to WriteHeader (or Write) has no effect unless the modified headers are trailers".

Collapse
 
moficodes profile image
Mofi Rahman

Thanks for catching that.

Fixed it.

Collapse
 
mrxinu profile image
Steve Klassen (They/Them)

There's a typo on this line:

log.Fatal(http.ListenAndServe(":8080", r))

Should be:

log.Fatal(http.ListenAndServe(":8080", api))
Collapse
 
moficodes profile image
Mofi Rahman
r := mux.NewRouter()

is my mux router. api is a subrouter on r.

In this example using api instead of r would work.

But if i have other routes on r this would not work.

Collapse
 
mrxinu profile image
Steve Klassen (They/Them)

Oops, good point. I think I hadn't used api yet and it was complaining about that.

Thread Thread
 
moficodes profile image
Mofi Rahman

one of the "best worst" feature of go. :D

Collapse
 
chinglinwen profile image
chinglin

I'd more concern about the following area

Api-doc generation ( go-swagger? )
The limit or rate control relate stuff.
How things integrate with auth token ( jwt stuff ).

Collapse
 
dirtyfishtank profile image
dirtyfishtank

Excellent article. Much appreciated!

Collapse
 
senseiwu profile image
Zenifer Cheruveettil

good one, it helped!

thanks

zen

Collapse
 
pvillela profile image
PVillela

Thanks for the excellent tutorial.

Collapse
 
allahthedev profile image
Neelesh

really awesome dude

Collapse
 
ajeyprasand profile image
ajeyprasand

bro thanks for the article however why did u did not add any code in the stuct called server initially ??

Collapse
 
moficodes profile image
Mofi Rahman
type server struct{}

func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
        w.WriteHeader(http.StatusOK)
    w.Write([]byte(`{"message": "hello world"}`))
}

You are talking about this part I believe.

For http.Handle it just needs some type that implements the handler interface.

For example, this below is valid code.

type hotdog int

func (s *hotdog) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
        w.WriteHeader(http.StatusOK)
    w.Write([]byte(`{"message": "hello world"}`))
}

func main() {
    var s hotdog
    http.Handle("/", &s)
    log.Fatal(http.ListenAndServe(":8080", nil))
}
Collapse
 
thejitenpatel profile image
Jiten Patel

New to go lang. What an article hats off 🙇