In this tutorial, we are going to walk through an example of how to create a Restful API with Golang. We are going to cover the initial setup of th...
For further actions, you may consider blocking this person and/or reporting abuse
Awesome tutorial.
I came to it because I got stuck in my own project. In my project I wrote a different swagger.yml file and then generated with
swagger generate...
command. This gavegen
folder and 2 folders inside, namelymodels
andrestapi
.Now I don't know how use the files generated, so I found this great article. I didn't know of Fiber, nor GORM, and they're awesome, thank you! I did your entire tutorial directly inside my project and it works great.
I was hoping though, that once I have that up and running, I will be able to replace "Todo" references with my "Group" and "User" stuff, but with those handlers.go, repositories.go and models.go it's a completely different structure.
Any idea how to go forward, given this poor description?
Hi Matej, thanks a lot for your feedback! I am glad you liked it.
Given your explanation above, I would actually suggest you to start by mapping your concepts into models like the todo model example. From there you create the repositories and handlers accordingly.
It is a bit hard to help without knowing much about your context, but I would gladly provide more help if you'd share more details about it. You can DM me on twitter if you want: @dev_pacheco.
I really would take you up on the offer, but @dev_pacheco can't be messaged.
I did make a copy of "todo" folder (module) and renamed it to "group". Also renamed everything in the files from
todo
togroup
,Todo
toGroup
, andTodoSomething
toGroupSomething
. This wouldn't immediately be OK, sinceGroup
was wrong, given my import"github.com/me/project/gen/models"
, so I had to replaceTodo
withmodels.Group
, and most errors were gone. It also automatically put an asterisk (*
) beforegroup.Name
infunc (repository) Find (id)
in theif
condition. Then I suppose I can just delete the "models.go" file in "groups" module. So now I think all it's left is to fixRegister
func to have the same basePath as in REST API, ie.And yes that actually worked! Thank you... I am curious though if I really need to set Router stuff here in the register manually like this, or can I get those methods set somehow from the REST API files generated from Swagger?
Also how does one write tests to make sure the database is connected, or a new Todo can be added?
Hey Matej, I am glad you could figure it out!
About the router, you can definitely setup differently, the Register func is just a suggestion. The idea about having this
Register
func is to have an unique way to initialize each of your modules.Definitely, it is a good practice to write tests.
I didn't include here because the article would get too long, but I could definitely create a part 2 of this article talking about tests, if that would be interesting.
I like your tutorial. It's nice, clear, easy to understand. Thanks.
hey i really love your article. how you explain is kinda concise and clear :). but the dockerfile section attract my attention. if you wouldn't mind, may I ask you a question? how the container is supposed to run when you only define ENTRYPOINT without CMD or other command related to running the built go application?
Hi @dryluigi, thanks for the feedback!
The command to run is defined in the
ENTRYPOINT
line.In the dockerfile we can specify how to run the app through either of these 2 options: CMD or ENTRYPOINT.
The ENTRYPOINT one is prefered if you have an specific built entrypoint to run the app, which in our case we have the
air
to run on dev mode and thetodo
built file if we want to run a production version. This option is good for apps that generate a build file, like Golang or Java apps for example.The CMD is used when you want to define a default command that can be overridden or extended by whoever wants to use this file. In Node.js applications for example you might have a:
CMD ['npm', 'start']
.this is really good. i haven't tried fiber yet. will definitely do now :)
Thank you @ujjavala ! Fiber is fun, I hope you also like it :)
I recently started learning Go and even with my basic knowledge I was till able to follow the articel, well structured
Thanks @thiago
I am so happy to know that, thanks for the feedback @geekmaros_23 !
Nice article! I enjoyed a lot! Thx! :)
Hey Fernando, thanks for your feedback!
I am glad you liked it :D