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

GIN

Get the most from the Gin HTTP framework

Discover how you can write secure and powerful HTTP-based services with the help of Mihalis Tsoukalos, a bit of Go and the Gin framework.

OUR EXPERT iis a systems engineer and a technical writer. He is the author of Go Systems Programming and Mastering Go, 3rd edition. You can reach him at www. mtsoukalos.euand @mactsouk.

Calm down vicar. The gin we’re talking about is an open source web framework (a collection of libraries and APIs) written in Go that can help you develop powerful HTTP services. All the code discussed here can be found in the https://github.com/mactsouk/LXF.285 GitHub repository. If you want to download all the code for this tutorial from GitHub then you’ll need to run the following three commands:

You need to run cd LXF.285 to go into that directory. We’re now ready to continue with the rest of the tutorial.

A basic server

One of the files found in the repository is called introGin. go and is located inside the /simple directory. introGin. go implements a simple HTTP server and is going to be used in this section to help you understand how to create projects with Gin. The important code in introGin. go is the following:

The first statement creates an Engine instance (with the Logger and Recovery middleware already attached) whereas the second one starts the HTTP server at the port number stored in the PORT string variable ( :8008 ). If you don’t specify the address the server will listen to, it’ll listen to all available network interfaces.

Because this is an empty web site, it does nothing and no URL can be served by it. Nevertheless, Gin works and the web server starts at the desired port number. Running introGin.go from within the simple directory requires executing the following commands:

Note that go mod init and go mod tidy should be executed only once to enable Go modules. Also, feel free to delete go.mod and go.sum at any time and recreate them. The go mod init and go mod tidy commands should be executed inside each directory of the GitHub repository before executing the relevant Go source files.

This shows the debug output when interacting with HTTPmethods. go. When a URL and HTTP method combination isn’t supported, the output shows a 404 HTTP code. Otherwise, it’s a 202 HTTP code.

There are two ways to test introGin.go. The first one uses a web browser and the second one involves a CL utility such as curl or wget. We’re going to discuss both Firefox and curl. When tested with Firefox (http:// localhost:8008/), introGin.go sends the 404 page not found message back because it has no support for any URL. Testing it with curl produces this output:

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
February 2022
VIEW IN STORE

Other Articles in this Issue


WELCOME
MEET THE TEAM
This issue Jonni’s being paranoid about malware. We wondered what’s the most paranoid thing you find yourself doing?
Threatscape Linux
If you weren’t taking your security seriously before,
REGULARS AT A GLANCE
German state to replace MS Office with LibreOffice
The switch will see 25,000 PCs move to open source software
Blender 3.0 released
Open-source 3D graphics tool receives major update
Man-uses-Linux fallout
After the well-publicised travails of a YouTuber trying to use Linux, the community bands together to help out
WHAT TO DO IN 2022
Matt Yonkovit Head of open source strategy and
ALARMING STUFF
Keith Edmunds is MD of Tiger Computing Ltd,
Proton: now even better!
Playing Windows games in Linux receives a big boost
Firefox sandboxing explained
Firefox 95 will come with RLBox to help boost security
Open 3D Engine now available
Open-source 3D engine will boost Linux game dev-work
Distro watch
What’s down the side of the free software sofa?
VENUS AND VULKAN
Antonio Caggiano is a software engineer based at
NEW AND IMPROVED
Jon Masters has been involved with Linux for
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
A diagram can save a thousand words, but
Alexander Tolstoy
is still zooming in and out his newly
FSearch
Version: 0.1 Web: https://github . com/cboxdoerfer/fsearch
Mapscii
Version: GIT Web: https://github.com/ rastapasta/mapscii
Telegrand
Version: GIT Web: https://github.com/ melix99/telegrand
Girouette
Version: 0.6.5 Web: https://github . com/gourlaysama/girouette
Sysmontask
Version: 1.0 Web: https://github.com/ KrispyCamel4u/SysMonTask
Luna Paint
Version: 0.11 Web: https://github.com/ lunapaint/vscode-luna-paint
AppImageLauncher
Version: 2.2.0 Web: https://github . com/TheAssassin/AppImageLauncher
Plop
Version: GIT Web: https://github.com/ Caltrop256/plop
Let’s Surf
Version: GIT Web: https://github.com/ jackbuehner/MicrosoftEdge-S.U.R.F
QOI
Version: GIT Web: https://github.com/ phoboslab/qoi
Xplorer
Version: 0.2 Web: https://github.com/ kimlimjustin/xplorer
DON’T TRUST YOUR VPN!
We lift the veil on the VPN world with the do’s and don’ts of what you should trust them with
REVIEWS
Easyspace web hosting
Mike Williams assesses a skilled UK host famous for its speed…
Intel Core i5 12600K
This is the best processor of 2022 so far for tech lovers and early adopters says Jacob Ridley, who’s looking forward to see what else the year will bring
Endless OS 4.0
Five minutes with the latest version of this distro and Mayank Sharma is reminded of the old adage: “The whole is greater than the sum of its parts.”
4MLinux 38.0
Any distro that’s survived for over a decade must be doing something right, but Mayank Sharma wasn’t prepared for what he saw inside this one…
Calculate Linux 22
Jonni Bidwell was dubious about making Gentoo easy, but after careful calculation of this KDE Edition he’s all for the idea…
Stellaris
Management hates anyone going anywhere, boldly or otherwise, so Phil Savage is in a space pickle as he’s only gone and taken over the galaxy
ROUNDUP
GUI backup tools
WE COMPARE GUI backup TONS OF tools STUFF SO YOU DON’T HAVE TO!
The Verdict
GUI backup tools
COMBAT MALWARE!
Jonni Bidwell wants to turn the tide on ransomware in 2022. It appears he has his work cut out for him…
TOP OF THE FOSS
CLOUD SOVEREIGNTY
Jonni Bidwell reveals how Nextcloud has become the poster-child not just for self-hosting, but for how to be a sustainable, successful FOSS project
Pi USER
Raspberry Pi Trading seeks stock flotation
Looking to boost investment, an initial public offering would inject up to $500 million into the business
Display HAT Mini
Les Pounder thinks that Pimoroni’s latest display was designed for ants, or at least younger people with better eyesight than him
Build an embedded temperature sensor
Les Pounder is feeling the chill this winter, but just how cold is it in his workshop and should he invest in a woolly hat, complete with pom-pom?
Booting multiple operating systems
Christian Cawley examines the various ways you can boot your Raspberry Pi, from installing an OS to multibooting
IN-DEPTH
THE GUIX SYSTEM
Guix takes a novel approach to its package management. Mats Tage Axelsson guides you around it
TUTORIALS
Graphing disk usage
Shashank Sharma is a fount of knowledge and knows how to present the information. Which is why he appreciates tools that display data clearly…
Store your vital passwords securely
Nick Peers takes a close look at Swifty, an offline password manager with a slick interface and hopefully bright future
Emulating the Commodore VIC-20
Les Pounder takes a trip back to the 1980s to discover which computer Captain Kirk was using when he wasn’t at the helm of the USS Enterprise
Recreate electronic rock music effects
Fancy following in the footsteps of Hendrix or Clapton? Mike Bedford shows you how to add 70s-style electronic effects to your music tracks
Build an advanced networking test lab
Stuart Burns brings us up to date on his networking insights and covers expanding the networked virtual lab beyond the single host
Build a self-hosted Fediverse server
Big Social has you, monitoring 24 hours a day all year round. Learn how David Rut land breaks free into his own self-run Fediverse echo-chamber
How to set up a pro-level music studio
Michael Reed digs deeper into what’s possible in the realm of a Linux-based music studio, and discovers that the sky’s the limit
CODING ACADEMY
Lag is killing games
Tim Armstrong shows off Latency.GG , an open-source global internet measurement system for reducing lag and jitter in multiplayer games
Chat
X
Pocketmags Support