Golang, Gin, Help a poor old non-dev!

Associate
Joined
25 Oct 2002
Posts
2,298
Location
Sarf Lahndahn
Background- I've been working in IT for 30 years, I can script until the cows come home. I'm fine with procedural coding. I can even pass data in and out of simple functions and objects. I'm fine with infrastructure as code and I can build a half decent devops pipeline.

But.....I've been looking to create a REST API for an actual business purpose, and Golang with Gin has been suggested. And I'm stuck at square 1 with an example from the Gin documentation.

I get that we're calling a gin method to instantiate a router object "r", and then we're defining a handler for the root endpoint... and r.Run() is keeping the main function running.... but everything past the "/" has flummoxed me... what are we passing to what?!

func main() {
r := gin.Default()
r.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"data": "hello world"})
})
r.Run()
}

I've read and re-read the Gin docs, an online Tutorial and a couple of video courses, and all of them gloss over it. The rest of the team keep saying how elegant Gin is.....

Can anyone explain to me, as a non-developer, in simple terms, what is actually going on? I'm feeling old.... and dumb :D

TIA!
 
Associate
Joined
30 Apr 2004
Posts
50
Don't know gin, but i'm guessing that the r.GET method is defining what happens when a HTTP Get request is sent to your endpoint (with a path of "/").

The func is a callback which is being used to set the reponse returned from the endpoint, in this case sending a 200 status in the header and some JSON in the response body.
 
Associate
OP
Joined
25 Oct 2002
Posts
2,298
Location
Sarf Lahndahn
LOL, yep!

I grew up with procedural languages (BBC BASIC first!), as part of the job I've done a lot of scripting (DOS, bash, Powershell) and I've played with Javascript. Other than that, Infra-as-code stuff like Packer, Terraform.

I realise that all those are really scripting, i.e. start at the top and end at the bottom, and a more object-oriented approach isn't something I can expect to just pick up and run with in a few days, but I'm wondering if my brain just isn't modern enough :D
 
Associate
Joined
24 Oct 2014
Posts
387
Location
South coast
I'm going the same way as @peterwalkley was gently pointing - unless you need the extra performance of a Golang solution, if you've got scripting experience and played with JS, why not do it with that? There are _loads_ of tutorials all over the web and you'll get the job done quickly.
 
Associate
OP
Joined
25 Oct 2002
Posts
2,298
Location
Sarf Lahndahn
Yeah, kinda....

Did a session with a couple of our devs and their advice was "Don't worry about it, just copy/paste code from the documentation and tweak".

With that advice in mind, and cribbing from the docs and a couple of tutorials, I've got our REST API up and working. Should have it doing enough to work in production in a week or so. It an internal API only, so it doesn't have to be coded defensively at this stage.

Maybe it's just me, but I'm not sure that ORMs actually make things easier to code from scratch. More portable maybe, but honestly if I'd started off with basic Go logic and raw SQL to the back end, I'd have got this done over a week ago :D
 
Back
Top Bottom