You are currently viewing the United Kingdom version of the site.
Would you like to switch to your local site?
15 MIN READ TIME

REST API

Create a RESTful server in Go

Learn the theory behind REST as Mihalis Tsoukalos explains how to develop a concurrent RESTful server in Go.

Part One!

Don’t miss next issue, subscribe on page 26!

OUR EXPERT

Mihalis Tsoukalos is a systems engineer and technical writer. He’s the author of Go Systems Programming and Mastering Go. You can reach him at @mactsouk.

QUICK TIP

The official Go web sites are https://golang. org and https:// go.dev. You can also read The Go Programming Language by Alan Donovan and Brian Kernighan as well as Mastering Go by yours truly!

This month’s tutorial covers how to go about developing RESTful servers in Go. The second part will show how to develop command line clients to access this RESTful server. Remember that the single most important task of a RESTful server is the definition of the REST API. With that in mind, let’s begin learning about RESTful services.

REST, or REpresentational State Transfer, is an architecture for designing web services. REST isn’t tied to any OS or system architecture and isn’t a protocol; however, to implement a RESTful service, you need to use a protocol such as HTTP.

Additionally, REST can work with any data format. Usually REST means JSON over HTTP. There are also times where data is exchanged in plain text format, usually when the exchanged data is simple and there’s no need for using JSON records. Therefore, when developing RESTful servers in Go, you need to create the appropriate Go structures and perform the necessary marshalling and unmarshalling operations for the exchange of JSON data.

Go for develop

Go is ideal for developing both servers and clients for REST APIs for many reasons. Go works well with JSON data and supports concurrency by design, to name but two. As a result, all RESTful servers are concurrent without needing any extra code because the packages used for serving client requests operate concurrently. However, the principles of concurrent data sharing still apply and you shouldn’t share variables carelessly.

The first version of the server is going to use packages of the standard Go library. The second RESTful server is going to use an external Go package that’s much better than the default Go package. This first version, which is used of illustrating the ideas behind the development of RESTful services, is going to support the following endpoints:

>/time: this endpoint returns the current date and time to the client. It’s mainly used for testing that the server and the client can communicate without issues. /insert: this enables you to insert a new record to the server. As a result, it requires a JSON record as input.

>/list: this last endpoint sends the contents of a map with the data to the client.

>Finally, there’s a default handler for all endpoints that don’t match an existing one, which in the default Go router is associated with the / endpoint.

Unlock this article and much more with
You can enjoy:
Enjoy this edition in full
Instant access to 600+ titles
Thousands of back issues
No contract or commitment
Try for 99p
SUBSCRIBE NOW
30 day trial, then just £9.99 / month. Cancel anytime. New subscribers only.


Learn more
Pocketmags Plus
Pocketmags Plus

This article is from...


View Issues
Linux Format
November 2021
VIEW IN STORE

Other Articles in this Issue


WELCOME
Windows Insider
So after saying it would never exist, Microsoft
LINUX FORMAT
MEET THE TEAM
REGULARS AT A GLANCE
Newsdesk
THIS ISSUE: Sir Clive Sinclair Steam Deck improvements WSL malware EU RISC-V chips Old Ubuntu gets extended support
GAMING
Linux games get faster
SECURITY
WSL malware discovered
THE MOST WANTED
PostgreSQL is currently ranked the “most wanted” database
SINKING FEELING
I was talking to a company recently who
European RISC-V chip gets closer
Test samples of the EPI’s RISC-V processor have been delivered
Linux 5.15 NTFS
The forthcoming kernel will come with a driver for better Linux support of the NTFS filesystem
Ubuntu 14/16.04
Both LTS versions have their lifecycles extended to an impressive 10 years
Distro watch
What’s behind the free software sofa?
STREAMY WINDOWS
GStreamer is a powerful multimedia framework with over
HITTING THE M1
Jon Masters has been involved with Linux for more than 22 years
KERNEL WATCH
Jon Masters summarises the latest happenings in the Linux kernel, because someone has to
Answers
Got a burning question about open source or the kernel? Whatever your level, email it to lxf.answers@futurenet.com
MAILSERVER
WRITE TO US Do you have a burning
HotPicks
Czkawka>> Cozy>> Tomatoid5 >> Scrcpy >> VMPK >> Biblioteq >> Timefind >> SuperTuxKart >> Fheroes2 >> Reptyr >> Hover Zoom+
Cozy
AUDIO BOOKS PLAYER
Tomatoid5
Version: 0.1 Web: www.pling.com/p/1574941
Scrcpy
Version: 1.18 Web: https://github.com/ Genymobile/scrcpy
VMPK
Version: 0.8.4 Web: https://github.com/ pedrolcl/VMPK
Biblioteq
ARCHIVING TOOL
Timefind
WEB ARCHIVE TOOL
SuperTuxKart
Version: 1.3 RC Web: https://supertuxkart.net
Fheroes2
Version: 0.9.7 Web: https://github.com/ ihhub/fheroes2
Reptyr
TERMINAL RE-PARENT TOOL
Hover Zoom+
Version: 1.0.180 Web: https://github . com/extesy/hoverzoom
On the disc
Discover the highlights from this month’s packed DVD!
Finnix 123
Never underestimate terminal-based file managers. 64-bit SYSADMIN’S DELIGHT
Lakka 3.4 & LibreELEC 10
LEISURE APPLIANCES MIN SPECS: N/A 64-bit S ince
AND MORE!
THE LXF LIBRARY ● Advanced Bash Scripting Guide
REVIEWS
REVIEWS
FireCuda 530 2TB
Asus Tinker Board 2S
This is the little board that couldn’t, says a disappointed Les Pounder as he tries a board that’s definitely no Raspberry Pi
Zorin OS 16 Core
A way to move operating systems, or a desktop with an identity crisis? Neil Bothwick pretends to be a Windows lover while he tries Zorin OS
Rescatux 0.73
With its fairly descriptive name for a distro, Neil Bothwick finds out in just what situations it can rescue our little penguin
Deepin 20.2.3
Deepin is an independent distro originating in China. Neil Bothwick tries it to see what he can take away from the experience
American Truck Simulator
Management is firm: no new pay rises. So they’re terrified that Andy Kelly is planning to retrain as a highly paid truck driver
ROUNDUP
Static site generators
Web development is a complex activity, but you can publish a simple website with the help of static site generators and Markdown files
Installation
You generally install using a package manager, but in some cases you cut and paste
Configure your project files
To make a great webpage, you need the project files to be well structured
Usability
Using generators helps set the style
Programming language
Your choice may be dictated by the language
Frameworks
In some cases, you can use web frameworks with these static generators
Platform support
Things are always easier with a bit of support
Updating sites
Easy updates are vital
The Verdict
Static site generators
LINUX WINDOWS
Old rivalries have been forgotten and ancient boundaries blurred. Jonni Bidwell investigates this new Redmond-Penguin harmony
Platforms, Trusted Platforms and Windows 11
Windows hardware requirements have always promoted confusion, sand this time is no different…
Linux in Windows
Never mind Windows 11, the latest version of the Windows Subsystem for Linux is where it’s at
Year of the Linux desktop (on Windows)
Running graphical applications is impressive, so the next step is to try and run a complete desktop. What are you waiting for?
Duelling OSes
Linux has always tried to co-operate with Windows oddities, and despite common gripes, it does an amazing job
Pi USER
Astro Pi 2021/22 launches with new kit
Seven super-successful years on and the Astro Pi models get a hefty upgrade for the next competition
Node-RED 2.0
Les Pounder enjoys building things, and with Node-RED 2.0 he can construct the Internet of Things directly from his beloved Raspberry Pi
Controlling the Pi’s GPIO with Scratch
Les Pounder shows how we can use Scratch with simple electronics
Setting atomic-clock precise time on your Pi
Sean Conway makes use of a Raspberry Pi Hat to pass some time, by setting up a GPS receiver to establish an accurate clock
IN-DEPTH
INSIDE THE INTEL 4OO4
Mike Bedford shows you how to use an emulator to get hands-on experience of the microprocessor that started a revolution
LINUX FOR THE SOUL
Jonni Bidwell wants a free, enterprise-grade operating system and word on the street is AlmaLinux is the coolest one going…
TUTORIALS
Create efficient backups
Backups aren’t just a safety net for Trekkies such as Shashank Sharma, but a way of life. But they can take up quite a lot of space if not done right…
Network-share files quickly and easily
Nick Peers reveals how to share files over your local network without getting bogged down in networking protocols
MOBILE SHARING OPTIONS
You’ll find additional sharing options are available when
Running the classic Tandy TRS-80
Les Pounder travels back in time to when Doctor Who had a mop of curly hair and a trio of computers ruled our homes
How to build the Linux Format server
Start running your own server with the help of David Rutland and our new series on what to do with one once you’ve got it!
Build your own custom Ubuntu distro
Why settle for what the existing distributions have to offer? Michael Reed looks at Cubic, a tool for creating your own custom respin based on Ubuntu
Improve your shots using optical filters
Mike Bedford looks at the benefits of photographic filters and how to emulate them – but some effects can only be achieved optically
CODING ACADEMY
Code a multiplayer shoot-em up game
In the first of a two-part series, Andrew Smith takes you though coding a multi-player shooter in good old Python
Chat
X
Pocketmags Support