How To Build A Complete JSON API In Golang (JWT, Postgres, and Docker) Part 2
Вставка
- Опубліковано 5 лис 2022
- In this series, I will teach you how to build a complete industry-ready JSON API project in Golang with JWT authentication, Postgresql, and Docker. We are going to build a bank API and build everything from scratch with only the mandatory packages we actually need. We are also going to write tests for each of our HTTP handlers. If you want to learn Golang or Rust consider subscribing to my channel so you can become a high-value engineer.
This golang tutorial is something for you if you are willing to learn about Golang testing, Golang project structure, and how to use Docker with Golang.
📢 I am also building a Discord community! Feel free to join if you are willing to learn about blockchain, web3, Defi, and quantitative trading. Or just hang out and chat.
/ discord
► Support me on PATREON and get access to exclusive lessons, tutorials, and LIVE streams
/ anthonygg_
► Join my Discord community for free education
/ discord
► Follow me on GitHub
github.com/anthdm
► Follow me on Twitter
/ anthdm
#golang #docker
👉 Join the Discord community for blockchain, distributed systems, Golang, and Rust education:
discord.gg/bDy8t4b3Rz
👉 Support me on Patreon for even more exclusive videos, vlogs, and tutorials:
www.patreon.com/anthonygg_
The code can be found on GitHub: github.com/anthdm/gobank
Thanks for watching
That is the best tutorial I have seen in my life, this is actually what happens and how we build applications. All tutorials that I have watched all examples were already preprogrammed, and the professor never shows how it is real process of building application. I really loved these 5 Go videos.
About the "_" (underscore) in imports - Generally used when you want to load a library and run its scripts without having anything to do with what it returns.
Great series of tutorials. You are amazing!
You are a very energetic teacher. Thank you for your videos 🔥
Your welcome!
I love the way you teach. You keep the vibe as casual as possible that I enjoy the lesson till I finish this video. Keep it up man
Ty
In a few hours, I followed the first two videos today. I tried to take my time to fully understand and it's really incredible how much we did in two videos!
You're a killer Anthony!
"you are a package slave, you don't know shit"
*googles SQL create table command*
haha love this series, you've got a great teaching style I'm learning a lot. BTW, I've got an annie pro 60% and the backtick is FN+esc on there
LMAO 🤣
Good exercise
Tip: If you create a type serial on your postgresql table, it actualy create a sequence to that column and the default value will be aways a nextval(''table_name_column_name_seq'), is really good for ID's and sequencials, but in this example for a balance, every time you insert a account if you not specify balance = 0, the balance will be the nextval of the sequence xD
Thanks for this video btw, you are helping me alot :D
Thanks Anthony. Great video!
Thank you 🙏.
_ package -> the underscore following the package is for side effect only with out calling any method from it.
Very helpful series, great job!
The problem was not with w.Header().Set() or .....Add() it was with you had to call w.WriteHeader(status) after setting content type.
Great content. Thanks.
Thx for the video man, really helping to absorb the experience for my go development.
When starting on a new language the best way to learn how to do things is to see how other people does, and not try to push my own way of thinking coming from other languages.
I can see a uniqueness in your videos. thank you for the videos.
Thank you for sharing those things! I'm not good at English, but I've finished making this project with your video!
Actually, I don't grasp how the framework works under the hood so far. These days, I'm trying to read the framework or library code to understand how it works under the hood. Thank you, Anthony. It's a really nice lecture I've ever seen before, To learn how to make a REST API Server with Go!
I wish that If I were good at English, I'll make the lecture like this!
great series, love the minimal use of frameworks/libraries
Awesome Video Anthony!!
Thanks again Anthony, great video!!
🤝
great tutorial, love the energy and style! subscribed hope you got the autocomplete fixed eventually in this series 💀
This man so fun, I love your videos. Thanks
Thanks a lot......
You are as awesome as your contents....
Thanks again....
Great content!
You are really magic person. Thanks Anthony. I wish I could be a premium member and could buy your course.
Anyhow, really really appreciate your efforts. I'm learning a lot.
Anthony is funny as hell.... "Don't be package slave , lets say the world explode and there are no packages anymore then you screwed". ... so so true
i could not stop laughing. ..... kiakiakia kiakiakia. .... love u man
Thanks man. Impressive stuff.
Thanks a lot for making this series, I've enjoyed it and it's helped reinforce some things for me (between drinking many margaritas)
🍸
I love when you made typo and say "what's going on here" 🤣
btw thankyou so much for the tutorials!
Buddy i've learnt lotta things from you ty! btw, it's an interesting rule. first make it to work then make it better ultimitly make it fast.
Thanks, Antony!
Thank you!
Get it work, make it better, then make it work faster 🙇♂🙇♂, loved the series, Thanks @Anthony
Yooh Anthony, amazing video and content like always!! By the way the thing ` in the 60% key board you get by using Fn + ESC, at least thats in mine a 60% US Layout and I use the USA international for the language lol
Cheers and thank for the video!!
Wow thanks! Searching for this for a long time.
I know this is ultra noob but I'm commenting here incase somebody runs into this issue. Doing this exercise on windows and I constantly kept getting this error " pq: password authentication failed for user "postgres" ". Turns out I already had postgres (not through docker) running on port 5432 for another project and that was being accessed when I tried connecting to the docker postgres instance and hence the incorrect password.
Thank you, i forgot i got installed postgres in this machine, jesus haha
I actually liked it at 35:50 .. love goes out to Bobb Ross and all his fans ❤
we are importing the postgres driver like this because we are using the api provided by golang so the package job is to register the postgres driver to the go standard sql api
The problem with JSON was not because of Add or Set but because of the order of the functions calls. "WriteHeader SENDS an HTTP response header with the provided status code" - description from stdlib. So by the moment you set the header application/json, response is sent as I understand
the underline is actually to tell the compiler that you are not using the lib itself, just its side effetcs, under the hood the db.Sql uses the driver to connect with the postgres, we do not explict use it on our code xD
Great learning experience.
Any non-web based projects you recommend to get confidence in golang? I am picking up some Python book and trying to solve problems in Go. Many golang books don't have exercises..
Fabulous
The `` symbol is located (american layout) above the tab key, below the ESC and left of the 1 key.
Now we should all get idiomatic with Postgres 🐘 Anyway your presentation style makes me want just to get down to doing things and not seek the nonexistent perfect!
For create tables, you could consider db migration.
That white backgroung in your postman is killing my eyes. Since i switched to dark backgrounds I'm such a crybaby.
The backtick (`)is supposed to be the button right of the +/= button. If it's not there, it's the button left of the !/1 key.
` should be some modifier key and esc key on 60% us layout acording to google.
Hey Anthony, for the backtick try the following combination: Alt + 96
Hey man. Figured the tick out 🤣 fn esc on my keyboard. Thanks though!
for me, it is cmd+fn1+esc Im so scare every time I have to type this character LOL
15:36 What about using a migration tool to create your database structures? I use the Soda CLI and Fizz DSL from the Gobuffalo project with great success. Migrations are easy-peasy after the initial hurdle of learning the DSL.
Edit: _I already know how to write the queries in plain SQL, so if the objective is to learn SQL while you're also learning Go this might not be a good idea..._
I didnt find out why u just used pointer for accounts,
I saw ur video about pointers but now what is benefit of pointer recivers ? I think u can use value reciver
Not sure if this will be answered later but how do you differentiate between handleGetAccount and handleGetAccountById?
Because the thing that kind of routes the endpoints to the function is the handleAccount function and there you say: If the method is r.GET, it should return handleGetAccount. Wouldn't that mean handleGetAccountByID is never used?
Have you done a part three with the JWT stuff
The conventions for create apis are: methods (post, get) domain/accounts and (get, put, delete, patch) domain/accounts/{id}
Hi, quick question would it be fine to modify Server struct like port, usersStore, transactionsStore, secrets. Can we break single store in multiple beacuse if we have 20 methods on interface it will be difficult to test it ?
Great tutorial. I know it's your setup but red/pink on black is too hard to see. I hope you'll change for video/user-friendly screen. Thanks!
it's time to put an end to package slavery!
Hello Anthony, Please how can i fix my response form text to jSON ? how did you fix it, you didnt tell us in the tutorial
Isn't there a difference between new(Account) and &Account that the &account is a pointer on the stack? and new on the heap?
That's a difference in C/C++ anyways and you may the be referencing a pointer that went out of scope. So I would opt for new from my C++ background. Or use & only for objects that are created in pain and pass around that pointer.
Looks like gorilla mux has been archived, will you be doing a new series on a new mux?
Not sure. Maybe :)
Thanks Anthony
Can we use fmt.SprintF() instead of `` ?
This programming language is a struct hell )
I don't understand a thing, why did you incapsulated db into PostgresStore type? The entire project would have worked anyway using directly db, no?
Not sure what you mean. But its probably for interface stuff later on. So you can also have a mongodb store. Something like that
@@anthonygg_ I was talking about the struct PostgresStore, btw yes understood thank you. Creating that struct you can gracefully handle the db connection in the NewPostgressStore func, and then of course you will be able to have several different stores such as MongoStore. Btw I really appreciate your coding style (even though i'm still learning) and your teaching style!
How do you tab all the lines above at 23:51
Why you use lib/pq? This library is deprecated and now jackc/pgx is prefer.
Aight!
this is save foor sql injection !!!!
when doing rows.next() dont forget to add rows.Close()!!!
Dayum, true true. Thanks for reminding me
May I ask why you name all your packages as "main"? Isn't it more easy to understand code and maintain it using different package names so that you can know which code comes from those? Just learning the lang.
he talks about it in the first part, I would add that it's far easier to split in modules once you have a good idea of the parts that your application is gonna involve, not before
For me the ` symbol is right alt and escape
For the blind homies T_T
Never actually showed how to get the new ID out of the database, or return the newly created account via the API. Feels like you lost heart in this. Shame
you're so funny man, such a joy learning from you
Love the stuff your teaching, but damn it's unbearable watching you. Just spamming keyboard to look fast, making more typos than actually right ones, like slow down man..
working through your examples, but having a problem with packages in different files, my normal structure...
go.mod
go.sum
main.go
I normally dump all code into main.go, but I'd like to maybe create a api packages and a types package and utils package
As I understand that would mean a api, types and utils sub directory under cmd and I then do import of "api" "types" and "utils"
issue, when done like this then the code in main.go that's trying to call the func's in api, types and utils errs out saying it can't find...
please helpppp
Keep it simple and dont use folders.
@@anthonygg_ tried that also. will try again.
... in the absence of folders, what do I import still the file name as the package name.
@@anthonygg_ ... moved to same directory, left them as package main.
tried importing the filename but as soon as I save it's removed by vscode ?
@@georgelza if in same package just call them by name without prefix