Dive into Go API
Photo by Jakob Owens on Unsplash
For several years now, I have been writing my programs daily in the Go programming language.
Well, actually, I also do quite a bit of assembly, a lot of C, OpenCL, GLSL, Verilog, some Cuda, Python, JavaScript, PHP and Shell, but for my projects involving a RESTful API and a server, I essentially use Go.
In particular, as my way of writing this type of code has evolved over the years, I have come to devise a RESTful API server development method that suits me well. I doubt it can meet everyone's needs, but perhaps it could inspire you?
In this article, I propose to share a small code template that serves as a foundation for almost all my HTTP API service projects.
Table of Contents
- Why RESTful, and what does it entail?
- Gorilla/mux for handling routes
- Should you use the encoding/json package?
- Which database to choose? (SQL, Key/Value, File)
- Securing requests
Why RESTful, and what does it entail?
There are several possible architectures for providing an HTTP API, including, among others:
- XML-RPC
- SOAP
- JSON-RPC
- REST
- gRPC
REST stands for "REpresentational State Transfer". This architecture was described by Roy Fielding in a dissertation for his PhD in information and computer science (no less).
I invite you to read this document. It is not uncommon to find differing views on what a REST API truly is, and I think it is quite reasonable to refer to the original document.
To put it simply, REST is an architecture that describes the relationship between client software and server software. On the server side, data is made available to the client through data representations in simple formats such as JSON or XML, for example. REST proposes to define how data is presented and how it can be modified through simple operations.
The REST interface provided must be uniform. This means your data is presented as resources and collections of resources.