This document provides an overview and introduction to developing RESTful APIs using the Go programming language. It begins with background on Go and describes it as a general purpose, open source language created at Google with built-in concurrency. It then discusses RESTful architecture and standards before diving into examples of building RESTful APIs in Go using common libraries like Gorilla Mux and frameworks like Negroni for routing and middleware. The document covers best practices for RESTful API development including resource design, HTTP methods, authentication, security, and JSON API specification.
3. About Me
Senior Software Engineer, Red Hat
1
https://golang.muthukadan.net
Baiju Muthukadan RESTful API Development using Go @nogenerics 3 / 30
4. About Me
Senior Software Engineer, Red Hat
FOSS Contributor (SMC, Koha, Zope, SaltStack, fabric8 etc.)
1
https://golang.muthukadan.net
Baiju Muthukadan RESTful API Development using Go @nogenerics 3 / 30
5. About Me
Senior Software Engineer, Red Hat
FOSS Contributor (SMC, Koha, Zope, SaltStack, fabric8 etc.)
Founded the Swathanthra Malayalam Computing (SMC) project in 2001
while studying at REC Calicut (NIT Kozhikode)
1
https://golang.muthukadan.net
Baiju Muthukadan RESTful API Development using Go @nogenerics 3 / 30
6. About Me
Senior Software Engineer, Red Hat
FOSS Contributor (SMC, Koha, Zope, SaltStack, fabric8 etc.)
Founded the Swathanthra Malayalam Computing (SMC) project in 2001
while studying at REC Calicut (NIT Kozhikode)
Received the first Kenneth Gonsalves Award for contributions to the
Python community in India
1
https://golang.muthukadan.net
Baiju Muthukadan RESTful API Development using Go @nogenerics 3 / 30
7. About Me
Senior Software Engineer, Red Hat
FOSS Contributor (SMC, Koha, Zope, SaltStack, fabric8 etc.)
Founded the Swathanthra Malayalam Computing (SMC) project in 2001
while studying at REC Calicut (NIT Kozhikode)
Received the first Kenneth Gonsalves Award for contributions to the
Python community in India
Author of the book: A Comprehensive Guide to Go Programming 1
1
https://golang.muthukadan.net
Baiju Muthukadan RESTful API Development using Go @nogenerics 3 / 30
8. Quick introduction to Go
General Purpose Programming Language
Baiju Muthukadan RESTful API Development using Go @nogenerics 4 / 30
9. Quick introduction to Go
General Purpose Programming Language
Free/Open Source
Baiju Muthukadan RESTful API Development using Go @nogenerics 4 / 30
10. Quick introduction to Go
General Purpose Programming Language
Free/Open Source
Created at Google by Robert Griesemer, Rob Pike and Ken Thompson
Baiju Muthukadan RESTful API Development using Go @nogenerics 4 / 30
11. Quick introduction to Go
General Purpose Programming Language
Free/Open Source
Created at Google by Robert Griesemer, Rob Pike and Ken Thompson
Development started in 2007 and publicly released in November 2009
Baiju Muthukadan RESTful API Development using Go @nogenerics 4 / 30
12. Quick introduction to Go
General Purpose Programming Language
Free/Open Source
Created at Google by Robert Griesemer, Rob Pike and Ken Thompson
Development started in 2007 and publicly released in November 2009
C like syntax (no semicolons)
Baiju Muthukadan RESTful API Development using Go @nogenerics 4 / 30
13. Quick introduction to Go
General Purpose Programming Language
Free/Open Source
Created at Google by Robert Griesemer, Rob Pike and Ken Thompson
Development started in 2007 and publicly released in November 2009
C like syntax (no semicolons)
Object Oriented (Composition over inheritance no classes!)
Baiju Muthukadan RESTful API Development using Go @nogenerics 4 / 30
14. Quick introduction to Go
General Purpose Programming Language
Free/Open Source
Created at Google by Robert Griesemer, Rob Pike and Ken Thompson
Development started in 2007 and publicly released in November 2009
C like syntax (no semicolons)
Object Oriented (Composition over inheritance no classes!)
Compiled (Statically linked)
Baiju Muthukadan RESTful API Development using Go @nogenerics 4 / 30
15. Quick introduction to Go
General Purpose Programming Language
Free/Open Source
Created at Google by Robert Griesemer, Rob Pike and Ken Thompson
Development started in 2007 and publicly released in November 2009
C like syntax (no semicolons)
Object Oriented (Composition over inheritance no classes!)
Compiled (Statically linked)
Garbage collected
Baiju Muthukadan RESTful API Development using Go @nogenerics 4 / 30
16. Quick introduction to Go
General Purpose Programming Language
Free/Open Source
Created at Google by Robert Griesemer, Rob Pike and Ken Thompson
Development started in 2007 and publicly released in November 2009
C like syntax (no semicolons)
Object Oriented (Composition over inheritance no classes!)
Compiled (Statically linked)
Garbage collected
Statically typed
Baiju Muthukadan RESTful API Development using Go @nogenerics 4 / 30
17. Quick introduction to Go
General Purpose Programming Language
Free/Open Source
Created at Google by Robert Griesemer, Rob Pike and Ken Thompson
Development started in 2007 and publicly released in November 2009
C like syntax (no semicolons)
Object Oriented (Composition over inheritance no classes!)
Compiled (Statically linked)
Garbage collected
Statically typed
Strongly typed
Baiju Muthukadan RESTful API Development using Go @nogenerics 4 / 30
18. Quick introduction to Go ...
built-in concurrency
Baiju Muthukadan RESTful API Development using Go @nogenerics 5 / 30
19. Quick introduction to Go ...
built-in concurrency
Two major compilers: gc & gccgo
Baiju Muthukadan RESTful API Development using Go @nogenerics 5 / 30
20. Quick introduction to Go ...
built-in concurrency
Two major compilers: gc & gccgo
25 keywords (less than C,C++,Python etc.)
Baiju Muthukadan RESTful API Development using Go @nogenerics 5 / 30
21. Quick introduction to Go ...
built-in concurrency
Two major compilers: gc & gccgo
25 keywords (less than C,C++,Python etc.)
Classification (Capitalized are exported public)
Baiju Muthukadan RESTful API Development using Go @nogenerics 5 / 30
22. Quick introduction to Go ...
built-in concurrency
Two major compilers: gc & gccgo
25 keywords (less than C,C++,Python etc.)
Classification (Capitalized are exported public)
Fast build (in seconds)
Baiju Muthukadan RESTful API Development using Go @nogenerics 5 / 30
23. Quick introduction to Go ...
built-in concurrency
Two major compilers: gc & gccgo
25 keywords (less than C,C++,Python etc.)
Classification (Capitalized are exported public)
Fast build (in seconds)
Unused imports and variables raise compile error
Baiju Muthukadan RESTful API Development using Go @nogenerics 5 / 30
24. Quick introduction to Go ...
built-in concurrency
Two major compilers: gc & gccgo
25 keywords (less than C,C++,Python etc.)
Classification (Capitalized are exported public)
Fast build (in seconds)
Unused imports and variables raise compile error
Operating Systems: Windows, GNU/Linux, Mac OS X, *BSD etc.
Baiju Muthukadan RESTful API Development using Go @nogenerics 5 / 30
25. Quick introduction to Go ...
built-in concurrency
Two major compilers: gc & gccgo
25 keywords (less than C,C++,Python etc.)
Classification (Capitalized are exported public)
Fast build (in seconds)
Unused imports and variables raise compile error
Operating Systems: Windows, GNU/Linux, Mac OS X, *BSD etc.
CPU Architectures: amd64, 386, arm etc.
Baiju Muthukadan RESTful API Development using Go @nogenerics 5 / 30
26. Quick introduction to Go ...
built-in concurrency
Two major compilers: gc & gccgo
25 keywords (less than C,C++,Python etc.)
Classification (Capitalized are exported public)
Fast build (in seconds)
Unused imports and variables raise compile error
Operating Systems: Windows, GNU/Linux, Mac OS X, *BSD etc.
CPU Architectures: amd64, 386, arm etc.
Cross compilation
Baiju Muthukadan RESTful API Development using Go @nogenerics 5 / 30
27. Quick introduction to Go ...
built-in concurrency
Two major compilers: gc & gccgo
25 keywords (less than C,C++,Python etc.)
Classification (Capitalized are exported public)
Fast build (in seconds)
Unused imports and variables raise compile error
Operating Systems: Windows, GNU/Linux, Mac OS X, *BSD etc.
CPU Architectures: amd64, 386, arm etc.
Cross compilation
Standard library
Baiju Muthukadan RESTful API Development using Go @nogenerics 5 / 30
28. Quick introduction to Go ...
built-in concurrency
Two major compilers: gc & gccgo
25 keywords (less than C,C++,Python etc.)
Classification (Capitalized are exported public)
Fast build (in seconds)
Unused imports and variables raise compile error
Operating Systems: Windows, GNU/Linux, Mac OS X, *BSD etc.
CPU Architectures: amd64, 386, arm etc.
Cross compilation
Standard library
No exceptions (Errors are values)
Baiju Muthukadan RESTful API Development using Go @nogenerics 5 / 30
29. Quick introduction to Go ...
built-in concurrency
Two major compilers: gc & gccgo
25 keywords (less than C,C++,Python etc.)
Classification (Capitalized are exported public)
Fast build (in seconds)
Unused imports and variables raise compile error
Operating Systems: Windows, GNU/Linux, Mac OS X, *BSD etc.
CPU Architectures: amd64, 386, arm etc.
Cross compilation
Standard library
No exceptions (Errors are values)
Pointers (No pointer arithmetic!)
Baiju Muthukadan RESTful API Development using Go @nogenerics 5 / 30
30. Basic example
package main
import "net/http"
func main() {
http.ListenAndServe(":8080", nil)
}
Baiju Muthukadan RESTful API Development using Go @nogenerics 6 / 30
31. Serve content example
package main
import "net/http"
func homeHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World!"))
}
func main() {
http.HandleFunc("/", homeHandler)
http.ListenAndServe(":8080", nil)
}
Baiju Muthukadan RESTful API Development using Go @nogenerics 7 / 30
32. Gorilla Mux
URL router and dispatcher
http://www.gorillatoolkit.org/pkg/mux
https://github.com/gorilla/mux
go get -u github.com/gorilla/mux
Baiju Muthukadan RESTful API Development using Go @nogenerics 8 / 30
33. Gorilla Mux Example
package main
import (
"net/http"
"github.com/gorilla/mux"
)
func homeHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World!n"))
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/", homeHandler).Methods("GET")
http.Handle("/", r)
http.ListenAndServe(":8080", nil)
}
Baiju Muthukadan RESTful API Development using Go @nogenerics 9 / 30
34. Negroni Example
func main() {
r := mux.NewRouter()
r.HandleFunc("/", homeHandler)
n := negroni.New()
n.UseHandler(r)
http.ListenAndServe(":8080", n)
}
Baiju Muthukadan RESTful API Development using Go @nogenerics 10 / 30
35. Negroni Middleware
An exaple from
https://github.com/kaaryasthan/kaaryasthan/blob/master/route/route.go
n = negroni.New(negroni.NewRecovery(),
negroni.NewLogger(),
negroni.NewStatic(web.AssetFS()))
n.Use(middleware)
Middlewares: gzip, jwt, cors, csp
Baiju Muthukadan RESTful API Development using Go @nogenerics 11 / 30
36. Paths with variable
r := mux.NewRouter()
r.HandleFunc("/products/{key}", ProductHandler)
r.HandleFunc("/articles/{category}/", ArticlesCategoryHandler)
r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler)
Format: {name} or {name:pattern}
Baiju Muthukadan RESTful API Development using Go @nogenerics 12 / 30
38. RESTful
stateless
unique identification of resources through URIs
standard HTTP methods (POST, GET, PATCH, DELETE)
HTTP status codes
Baiju Muthukadan RESTful API Development using Go @nogenerics 14 / 30
39. Why RESTful?
easy to understand & document
works on limited bandwidth
READs can be cached and hence reduces the bandwidth
Baiju Muthukadan RESTful API Development using Go @nogenerics 15 / 30
41. REST Style consists of ...
Resources
Verbs
Media types
Status codes
Baiju Muthukadan RESTful API Development using Go @nogenerics 17 / 30
42. Media types
Use JSON ( http://json.org )
or better JSON API SPEC ( http://jsonapi.org )
application/vnd.api+json
Baiju Muthukadan RESTful API Development using Go @nogenerics 18 / 30
44. Security
All attacks possible with web are applicable to REST API.
https://www.owasp.org
- Use TLS always
Baiju Muthukadan RESTful API Development using Go @nogenerics 20 / 30
45. Tooling
cURL is your friend!
Baiju Muthukadan RESTful API Development using Go @nogenerics 21 / 30
46. HTTP Methods
POST - Create
GET - Read
PATCH - Update
DELETE - Delete
Baiju Muthukadan RESTful API Development using Go @nogenerics 22 / 30
47. An example API end points
Create item - POST /items
Read a single item - GET /items/1
Read all items - GET /items
Update item - PATCH /items/1
Delete item - DELETE /items/1
Baiju Muthukadan RESTful API Development using Go @nogenerics 23 / 30
49. jsonapi.org
standard for representation of JSON responses
shared convention increase productivity through generalized tooling
Baiju Muthukadan RESTful API Development using Go @nogenerics 25 / 30
50. resource representation in JSON
{
"links": {
"self": "http://example.com/articles",
"next": "http://example.com/articles?page[offset]=2",
"last": "http://example.com/articles?page[offset]=10"
},
"data": [{
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON API paints my bikeshed!"
},
"relationships": {
"author": {
"links": {
"self": "http://example.com/articles/1/relationships/author",
"related": "http://example.com/articles/1/author"
},
"data": { "type": "people", "id": "9" }
},
...
Baiju Muthukadan RESTful API Development using Go @nogenerics 26 / 30
51. HTTP Status codes and Location header
If a POST request did not include a Client-Generated ID and the requested
resource has been created successfully, the server MUST return a 201
Created status code.
The response SHOULD include a Location header identifying the location of
the newly created resource.
Baiju Muthukadan RESTful API Development using Go @nogenerics 27 / 30
52. structure for errors
HTTP/1.1 422 Unprocessable Entity
Content-Type: application/vnd.api+json
{
"errors": [
{
"status": "422",
"source": { "pointer": "/data/attributes/first-name" },
"title": "Invalid Attribute",
"detail": "First name must contain at least three characters."
}
]
}
Baiju Muthukadan RESTful API Development using Go @nogenerics 28 / 30
53. Conclusion
Go is a great choice for RESTful API development
Baiju Muthukadan RESTful API Development using Go @nogenerics 29 / 30