DEV Community

Cover image for Learning Go by examples: part 2 - Create an HTTP REST API Server in Go

Learning Go by examples: part 2 - Create an HTTP REST API Server in Go

Aurélie Vache on July 21, 2021

In the first article we setted up our environment. Now, we will create our first application: an HTTP REST API Server in Go. Initializati...
Collapse
 
cadayton profile image
Craig Dayton

Correction the cloned instance of go-rest-api is working. It was my effort to duplicate this setup in a localized project folder where the issues come into play.

Collapse
 
aurelievache profile image
Aurélie Vache

Hi, so is it working finally?

Collapse
 
cadayton profile image
Craig Dayton

Yes the downloaded git version of go-rest-api is working. It is when I attempt to create a local copy that I run into issues. I'm struggling with what needs to be a module versus just a regular local package and getting the import references working correctly. I've yet to see any clear documentation regarding the use of modules versus just local packages and how to structure project correctly. So, I'm spinning my wheels in attempts to come to a self understanding regarding modules in the combination with imports. As a beginner, the last thing I want to do is to start pushing worthless code to GitHub. I'm on POP!_OS and using VScode. VScode seems to be getting in the way at times too. Anyway, thanks for making available various examples I've learned a lot from them. I'll continue my struggle with Golang, but I seriously do not understand why it has to be so complicated.

Collapse
 
streamdp profile image
Alexandr Primak • Edited

Thank you very much! This is a great course. love gopher

Collapse
 
haikal00 profile image
haikal00

Thank you so much!

Collapse
 
arroyoruy profile image
Dan Arroyo

Thank you very much. I learned a lot. My dev environment is Ubuntu and had problems installing Swagger. Able to use docker method to run it, but in Taskfile.yml I had to change 'cmds' to included the entire docker run command. Otherwise no other issues. I can't wait for next one. also Definitely blues-gopher is my favorite one.

Collapse
 
aurelievache profile image
Aurélie Vache

Thanks Dan for your feedback 🙂.
I had problem in the past with installing swagger in a VM With Ubuntu too (4-5 years ago).

Yes using sicker image is a good solution and with Taskfile you can add or edit tasks it's a good way 👍

Ohh you like the blues gophers 😊 I love them too, the color match very well with gophers.

I'll publish another article next Wednesday I think 🤞💪

Collapse
 
houcemabdellatif profile image
Houcem Abdellatif

Thank you, good content. I run into a problem when generating files. When running the cmd: "task swagger.gen". did not generate the files needed. I got the error msg: "go: not generating in packages in dependency modules". any help

Collapse
 
pdevgl profile image
Mr Poyla

In Taskfile, you could just replace "github.com/scraly/learning-go-by-examples/go-rest-api/internal" for your own module name.

Collapse
 
aiengineer13 profile image
AIEngineer13

im having the same problem im using the "task swagger.gen" i tried to swich de version of my language but im running to the same problem

Collapse
 
michellejae profile image
michelle

heads up, if your doing this on an apple m1 machine i believe you'll need to add "--platform linux/amd64" to the taskfile on the docker command. :)

also sorry aurélie i accidentally posted this comment to part 1.

Collapse
 
aurelievache profile image
Aurélie Vache

Thanks Michelle.
I didn't test on M1 for the moment, I'll do it and update the article .

Collapse
 
mielofon profile image
Alexey Ponomarev

Thank you very much.
I repeated this code following you. But I have a problem with URL localhost:8080
It return
{"code":404,"message":"path /gopher/ was not found"}
because 'required: true' in swagger.yml for /gopher/{name}:

Collapse
 
aurelievache profile image
Aurélie Vache

Hi, thanks, yes if you follow the "let's test our app" part, I test "/healthz" route, "/hello/aurelie" and "gopher/dr-who" route :-) .

Never "/gopher" alone.

The goal is to retrieve an existing gopher so you can test with a gopher name ;-).

Collapse
 
cadayton profile image
Craig Dayton

I've learned much trying to getting the tutorials to work, but all tutorials are failing to execute for one reason or another. This includes the tutorials cloned with git. The last tutorial was the go-rest-api and very much like the approach but not repeatable for me.
I'm just getting started with Golang and finding it very frustrating not being able to find functional tutorials. Anyway, when I'm more experienced I'll come back to these tutorials and see what my newbie issues were.

Collapse
 
cadayton profile image
Craig Dayton

localized copy of go-rest-api now working.

Issue 1: 'task gen' not generating server code in /pkg/swagger
change to /pkg/swagger and executing 'go generate gen.go' worked.

Issue 2: Had to manually set import statement in server.go to match the git version

Issue 3: go.mod had to be manually created to match the git version.

VScode is issuing a warning that 'io/ioutil' has been deprecated since Go 1.16

Collapse
 
aurelievache profile image
Aurélie Vache

💪
thanks for your remark about io/ioutil package:
pkg.go.dev/io/ioutil

Collapse
 
julianperezpesce profile image
Julián Pérez Pesce

Amazing!!! Thank you again Aurélie

Collapse
 
tsouhaieb profile image
tsouhaieb

@aurelievache thank you, i think you need to add the command

in Let's build our app...
i think you need to add
go mod tidy

before

go build -o bin/go-rest-api internal/main.go ,

Peace out

Collapse
 
prondubuisi profile image
Onyemenam Ndubuisi

I am getting started with go and I have learnt a lot including best practices using your example. Kudos.

Collapse
 
aurelievache profile image
Aurélie Vache

Thanks for this kind comment ♥️

Collapse
 
codeanit profile image
Anit Shrestha Manandhar

I believe that you are a good technical leader and mentor Aurelie. To explain in an easy way is one of the most challenging part of being a good leader! Enjoying your post! Keep them coming! Cheers,

Collapse
 
aurelievache profile image
Aurélie Vache

Thanks Anit 🥰