How To Structure Your Go App - Full Course [ Hex Arch + Tests ]
Вставка
- Опубліковано 14 чер 2024
- Learn how to structure your go/golang/go programming language applications using the #hexagonalarchitecture
🌟 Hey devs! 🌟
Ready to level up your coding game? Check out this awesome course on Microservices using Go!
🚀 From beginner to pro, learn to build scalable apps with ease. Don't miss out!
Enroll now: 👉 kantan-coding.teachable.com/p...
#golanguage #Microservices #CodeWithConfidence 🔥🎉
📚Learning Objectives: Hexagonal Architecture/ Ports & Adapters, Go Programming, gRPC, Docker, Docker Compose, Dependency Injection (DI), Inversion of Control (IoC), Unit Tests, End To End Tests
👨🏽💻Code: github.com/selikapro/hexArchG...
⌨️ grpc_entrypoint.sh: github.com/selikapro/hexArchG...
☠️ Course Contents ☠️
🔎 (0:00:00) Intro
🔎 (0:02:20) What Is Hex Arch?
🔎 (0:08:57) Install Go
🔎 (0:12:29) Domain Layer
🔎 (0:22:50) Application Layer
🔎 (0:31:05) Framework Layer (Database)
🔎 (0:46:25) Framework Layer (gRPC)
🔎 (0:48:32) What Is gRPC?
🔎 (0:51:20) gRPC/Protoc - Install & Setup
🔎 (1:02:10) .proto files
🔎 (1:17:25) Framework Layer (gRPC) - Continued
🔎 (1:35:50) Bringing It All Together (main.go)
🔎 (1:44:06) Dependency Injection (DI)
🔎 (1:45:32) Visualizing Our Hex Arch Implementation
🔎 (1:50:49) What Is A Docker Container & Docker Compose?
🔎 (1:54:46) Install Docker
🔎 (1:56:00) Create Dockerfile
🔎 (2:01:32) Create Entrypoint File
🔎 (2:04:36) Create Docker Compose File
🔎 (2:15:56) Create SQL Script For Tests Table
🔎 (2:17:01) Add Unit Tests
🔎 (2:22:02) Add End To End Tests
🔎 (2:37:07) Running Tests Using Docker Compose
🔎 (2:43:39) Ending Notes
🔎 (2:44:50) Outro
📚🤓 References:
alistair.cockburn.us/hexagona...
www.dossier-andreas.net/softwa...
fideloper.com/hexagonal-archi...
blog.gougousis.net/the-not-so...
dzone.com/articles/hexagonal-...
en.wikipedia.org/wiki/Hexagon...)
• dotGo 2019 - Kat Zień ...
netflixtechblog.com/ready-for...
speakerdeck.com/fideloper/hex... Join the Discord to talk to me and the rest of the community!
/ discord
🌟 Hey devs! 🌟 Ready to level up your coding game? Check out this awesome course on Microservices using Go! 🚀 From beginner to pro, learn to build scalable apps with ease. Don't miss out! Enroll now 👉 kantan-coding.teachable.com/p/early-bird-special-pricing
#GoLang #Microservices #CodeWithConfidence 🔥🎉
I was introduced to Go 2 months ago and was having trouble finding resources. This video is great! It should reach more people, thank you for your efforts.
Thanks a lot 🙏 I’m glad it helped you out!
Wow!! This tutorial is so underrated. You deserve more recognition!
Thank you for your kind words🙏 I’m glad that you liked it!
Great to have you in this UA-cam community ! A lot of effort was put into this video , great
Thank you! I’m glad that you liked it 😆
This tutorial on hexagonal architecture is pure gold! As someone who comes from the PHP world and has experience building websites with my own MVC framework, I recently switched to Go and microservices and was struggling to find an effective way to structure my files. This tutorial provided me with a clear and concise approach to organizing my code. Thank you for sharing this valuable resource!
No problem! I’m glad that it helped you out. I too had a similar experience a few years back so it’s great to hear that this was helpful for you!
Thorough explanation. Much more than the title says. Great work especially explaining GRPC. Give us more.. I have a love hate relationship with the pattern. Mainly hate the complexity and obscurity. Great vid. Thanks for sharing!
Thank you for your feedback! I'm really glad you found it interesting 😀
This is gold! Thank you for such a comprehensive tutorial
My pleasure! I’m glad you enjoyed 🙂
A very good video. Excellent pace and direct delivery.
Thanks a bunch.
Thanks for your feedback. I’m really glad to hear that. That’s what I was hoping to achieve so I’m glad you think so 🙂
Rewatching this video many times. Leaened much from it. Special thanks for gRPC testing and testing in docker compose
I'm really happy to hear that! I'm glad the time and effort I put into this video has been able to help so many people. More Go videos coming soon 😊
This is a nice extension in domain-driven development. My services normally have the domain and application layer. introducing the framework layer is something I never thought to do.
Full process tuts are the best ) Thank you so much!
You're very welcome!
One of the best video I had seen about code architecture. Great explanation.
Thank you very much. I’m happy to hear that! 😊
@@kantancoding It's not just code architecture, the way you explained docker is also very much appreciated. Thanks for the knowledge. I wish you make more and more videos.
Thank you for uploading this video...... I was looking to learn golang and this looks a perfect fit for that :)
No problem! I hope that it helps you out 🙏
You are great man. I am glad that I have been able to discover your channel
Thank you! I’m happy to help. Thanks for your encouraging words 😊
Amazing i want to watch over and over, so clear and easy to reproduce
Glad you enjoyed it! Happy to help 🙂
Thank you Kantan! Amazing tutorial!
Thank you! I’m glad you liked it 🙏
Very nice course, keep going!
Thank you it was very helpful. Awsome tutorial!
Thank you! I’m glad you enjoyed it 😆
Re-Watching it, thorough explanation. Thank you
Awesome to hear that you are rewatching. Thank you 😊
Awesome tutorial! Subbed
Thank you! I've been enjoying a couple of your tutorials as well
You are a talented teacher! Very nice tutorial.
Thank you 🙏 I’m glad you liked it 🙂
very helpful. thanks for sharing
Thanks a lot! Really amazing!😀
Thank you! Really happy to see the effort is appreciated and helping others 😊
MAN, you are something special
Thanks for your kind words 😊 I’m happy to help!
My brother, this is going to be one fo the best tutorial chanel. Your delivery, coding skills and fluff free clean curriculum is commendable. Would love to work with you sometime :)
Hey bro. Thank you for your kind feedback! I’m really happy that my videos are able to help you out 😊
You earned a sub in the first 5 mins of this video. +1 from another gruvbox user.
Thank you sir 🙏
bless you man!
Really amazing tutorial 👍
Thank you! I’m glad you think so 🙂
Thanks a lot for this great tutorial
No problem bro. Anytime 🙂
I would edit some typing out. Implementing addition and watching/waiting for the copy paste distracts heavily from the topic.
Also can use a shorter interface as an example.
Great vid in general :)
awesome thanks for the video ❤
No problem!
Excellent, please don't give up you are doing very good job.
Thanks for the encouragement!
@@kantancoding Bro Golang is becoming popular I am glad you got in this train right now because right now and in the future you will be recognized as on of the best developer instructor on youtube of golang.
Please make a video advising developer like me to focus on frameworks and libraries to focus on to build things not non popular maintained ones, because it can be confusing because Golang is relatively a new Language thank you.
Great tutorial
Thank you! Glad you think so!
Thank you
My pleasure!
Thanks for the video.
I am wondering though, shouldn't the domain be the one to operate the persistance layer?
No problem! Can you elaborate a bit on your thought process?
Subscribed 🎉
Hope you enjoy the content!🙂
doing god's work🙏
Happy to help brother 🙂
I'll probably receive a lot of negative feedback, but from my humble opinion, hexagonal architecture is the healthiest way to implement DDD without failing. Cool video BTW! (and subscribed)
Also after zooming out at 36:00 looks better for me.
Thank you for your feedback! I’m glad that the video was helpful 🙂
Half way through and so far it was an enjoyable ride :)
But since this cost me a bit of time I just have to ask:
What's the version of protoc-gen-go-grpc you used? When I followed along I had to embed an UnimplementedArithmeticServiceServer into the rpc-Adapter-struct. Sadly (and unlike protoc and protoc-gen-go) that version isn't saved into the generated code. But I'm wondering since the other protoc-tools versions matched and I didn't see you using require_unimplemented_servers=false...
I actually don't remember what version I was using as I made this tutorial many moons ago 😂. But when you say you embed an UnimplementedArithmeticServiceServer do you mean that you are hardcoding it into the generated code? When you run the protoc command you should be able to do something like this --go-grpc_out=require_unimplemented_servers=false:.....
@@kantancoding ouch :D I shouldn't have stopped watching. 5 minutes later you explain all that
Great tutorial bro! Much love from Tanzania 🇹🇿. One question though, can’t we jus write the db retry logic in the app instead of having a separate sh script?
Hey thanks for watching! I think there are many ways to skin a cat 😂
Firstly, thank you so much for your time and effort in making such an illustrative video.
I followed your inputs and developed a RESTful application however got confused a bit. Can you give a pointer by creating same application replacing grpc with http?
Thank you, I’m glad you liked it 🙂 You can just add an additional interface using pkg.go.dev/net/http. Hope it helps!
One like for detailed walkthrough and another like for mastering Vi in 2021 👍
haha, vim is life...
Good stuff
Thanks! Hope it helped
Awesome, I love u
❤️
Hi Kantan, It was amazing video over the domain driven approach hex architecture microservices. Awesome !!
Can you please share your vim settings to get those autocomplete and nice display ?
Thanks! Glad you liked it! Here's a video I made explaining my vim setup: ua-cam.com/video/mbSaK3EOqO8/v-deo.html
@@kantancoding Thanks Kantan !! Appreciate you man !
@@kantancoding Sorry for troubling you, but the structure that you followed in the video doesn't match up with the git repo that you shared above. Is it possible for you to update the repo with the latest changes ? I see a lot of differences in there. Once again, awesome work man !! This is a piece of art beautifully explained !!
dbaseDriver := os.Getenv("DB_DRIVER")
dsourceName := os.Getenv("DS_NAME")
dbAdapter, err := db.NewAdapter(dbaseDriver, dsourceName)
Regarding the above, it would be quite good if these variables are resolved into the NewAdapter function because other databases can have different configuration and requirement like mongodb, sql, redis etc. I know it's just demo but if anybody got confuses, i would like to point above one.
Please correct me if i am wrong.
For Self Reference
8:36 Project Structure
Thank you...
My pleasure 🙂
what is ur Linux distribution and what is ur monitor?
Hey Thanks for sharing this video. while I'm watching this video, I'm also very interesting in your vimrc. could you share how did you config your vimrc for changing the status on VIM bottom while being Normal mode or Insert mode
Thank you 🙏 I have a video where I explain my whole vim setup here: ua-cam.com/video/mbSaK3EOqO8/v-deo.html
You're the best
❤️
grate video!! there aren't many go projects with this quality
why did you use vim instead of something like vs-code?
Thanks! I’m glad you liked it. I use vim for most small projects because it’s less cumbersome/faster than an IDE and half of the IDE features I don’t use when working on small projects.
For work/large projects I use an IDE 👌
May I know which terminal / theme do you use? looks stunning!
I use iterm2 and my color scheme is custom. You can configure custom schemes in the iterm2 settings. Just google iterm2 color schemes.
Thanks for the nice lecture! By the way, what's the font name of your terminal?
I actually forgot the name but it’s in the video on my channel about how to set up your terminal 😉
is this entire video one project or tutorials on different sections? also if it is one project, what does the application do?
If i wanted to have multiple db's (mongo/postgres/..etc) would I just change the 'type Adapter' to type MongoAdapter/PostgresAdapter? same with NewAdapter->NewPostgres/NewMongo
Give it a try! 😉
Is the database injected directly into the application layer? It doesn't go through the framework layer? Excellent video!
It's like application layer depends on framework and core... I don't get it
Hmm, so basically, you can see exactly which layers depend on which layers from the main.go file by looking at what is being injected where. It’s actually quite difficult to comprehend in the beginning but I think you are closer to understanding it than you think! I think this is a tutorial that you might have to watch certain parts more than once 😉
How should someone fit something like bi-directional streaming into their hexagonal architecture ? given that it streams both ways.
If a particular architecture doesn’t go well with your use case, it’s usually better to consider other options as well. This isn’t a one size fits all architecture imo.
recommended I would like this🤩 Much better than most paid courses out there. 👌👌👏
🤣 thanks a lot 🙏
I really love your tutorial!! I was looking for some good implementation of clean architecture or hex architecture and this One is so good!!
But... I have a question, you say: "We dont want inner layer to depend to outer layers.". So I dont understand why the app layer depend on dB witch is on outerlayer. We just need to do dependency injection in order to pass throught this limitation or maybe I misunderstood something? :)
Edit: ok I think I understand, instead of making outerlayer depend on innerlayer, we are just using dependency injection everywhere.
In this case how is the dependency of an outerlayer on an innerlayer represented in the code?
This question prevents me from using clean architecture or hexagonal architecture in good conscience 😅
When an outer layer imports an inner layer package and makes use of it to do what it needs to do then it depends on that inner layer. If that inner package is removed then the outer layer would break therefore it depends on that inner layer. Is that what you are asking?
@@kantancoding Okay, thanks! I think I understand :)
Instant subscription.
Althoughhhh...maybe the folders are a bit much? I mean, I understand we need them for different Go packages but still...
haha, I'm a bit of a folder addict. I just like things to be compartmentalized. The folder structure is somewhat flexible in my opinion so feel free to change it in a way that makes the most sense to you.
@@kantancoding Thanks for the reply! When I went through my followed-along version again, I found it helpful in remembering the roles of everything, so YES!
what is your theme? I wanna try it out myself, looks like some kind of soft gruvbox variant :)
Yeah it’s gruvbox👍🙂
Shouldn't the app and core directories be outside the adapters directory?
The directory structure isn’t set in stone. I think, use this as a guide but discuss with your team parts that you might want to change to fit the needs of your project.
Hello, awesome video, keep going with this :) I have a question. Any sugestion about mac with apple silicon M1 pro ? I have errors with sh script and i cannot deal with it :/
Thank you! Glad it helped. What are the errors?
@@kantancoding Thank you for your response, I cannot send any images in comment section, so I send you image via instagram :)
@@kacpereczynski613 it’s better if you join the Discord and ask your question there so that others that run into a similar problem can see the discussion 🙂
@@kantancoding Sounds good, where i can find any link to discord ? :)
@@kacpereczynski613 If you go to my channel page and select the Community tab, it should be in the top post
When i generated rpc server via proto it does not have any handlers or register server function
What is the error?
Bro can you please make a video series on using vim as a code editor / IDE and why this would be better than using full bloen IDEs like jetbrains products? Would really appreciate it
Actually, using the text editor vim lacks many of the out of the box features provided by IDEs. You can have a pretty feature rich vim setup but it takes a lot of configuration.
I actually don’t think one option is better than the other. I use vs code and jetbrains products with vim plugins every day and they make my life easier.
I also use vim over an IDE in many situations as well. It really depends on what I’m working on.
For Java I heavily depend on IntelliJ. For Go I usually just use vim. So yeah, just use whatever tool makes you the most productive. I’m not part of any camp and I don’t advocate for either. They’re just tools to me.
I will say that learning vim will definitely increase your productivity regardless of if you use it in an IDE or in the normal text editor.
Hi, I am just getting started with GO, I have learned the basics of it and currently searching for resources to get more hands-on with it, Is this tutorial suitable for beginners to get a better understanding and hands-on with GO?
Hey what’s up! Honestly, just try and go through it. There are many different types of beginners. I try to explain everything in detail but I can’t really give you a for sure answer about whether or not you can handle this course because I don’t know your exact level. If you struggle after the first hour then maybe you need to try some more beginner friendly material.
I have a beginner friendly video that will walk you though making a payment processing backend. It’s called introduction to programming using Go. I think you should try that one as well.
@@kantancoding definitely, I will go with your advise...thanks for replying though! BTW I really admire your teaching skills, I finally understood time complexity after watching your videos, I wish I could be as good as you are in terms of coding.
😂 I know many people that are much better than me. Just keep at it and you will get there in no time! Also, thank you for the feedback. I really am happy to hear that 😊
Hello my brother, how are you? Man, are you use VIM or NEOVIM to code?
What’s up bro. I’m using vim 😎
@@kantancoding Ooooooh my Gosh, I found your .vimrc config on your GitHub, thanks so much for sharing.
@@arthurneto1307 no problem. I also have a video on how to set it up😉
hello sir,
can you please make a dsa playlist for golang
Hello sir. You will find that when it comes to DSA, there is really no difference between languages. For example, here is binary search in Go:
package main
import "fmt"
func main() {
fmt.Println(binarySearch([]int{-1, 0, 3, 5, 9, 12}, 3))
}
func binarySearch(nums []int, target int) int {
start := 0
end := len(nums) - 1
for start target {
end = mid - 1
}
}
return -1
}
And here is binary search in Python:
def binarySearch(nums, target):
start = 0
end = len(nums) - 1
while start target:
end = mid - 1
return -1
if __name__ == "__main__":
print(binarySearch([-1, 0, 3, 5, 9, 12], 3))
As you can see, the algorithms are exactly the same. The only difference is language syntax. You will also notice that if you can read one, you can read the other. So when learning dsa, don't worry so much about the language being used. If you find materials, that are helpful that use a different language from the language you're most comfortable with, odds are you can still understand the material if the teacher is doing what he/she is supposed to be doing.
Is there a book I can use or another resource I can use to learn this? The tutorial is pretty good and I'm still having trouble following the "why" of what we're doing. I feel like I'm just following steps without understanding anything. Again, not your fault. I just can't follow things without really understanding the deep "why" between each step.
I think maybe this might be a bit advanced for you in that case. It’s hard to say without knowing what parts in particular you are having a difficult time understanding. Maybe I can make some suggestions if you provide that info 🙂
@@kantancoding I just didn't understand the naming conventions and what they really meant or why they were needed. After some time, I think I'm ready to try this video again. Thank you for the reply!
Curious why don't use pointer for receivers?
Hey, what’s your question exactly? There are many things I didn’t use including go routines.
Amazing tutorial, but seemed that you changed the architecture a bit in the repo to separate application layer
Thank you! Here’s the video that explains the change in the architecture:
ua-cam.com/video/C-GBzD5Qk2A/v-deo.html
@@kantancoding Cheers
What would the structure look like if structs were involved? For instance, what would a port interface look like if a method signature consumes a struct? Would the struct type go inside the ports package? I'm assuming it would since it would be odd (imo) to import a struct type from the framework layer back up to the port layers. As you mentioned, everything should be going inwards. At the same time, importing structs from the ports package also seems a bit strange because of the naming. For example, ports.Arithmetic{op Operator, result int} -- this struct would be best inside the core arithmetic package, but again, you'd have to import "upward" if you needed to make a method signature in the ports interfaces that consumed this struct. Maybe I'm missing a better design here, but that's the only con I can see here, what are your thoughts on how to do this?
In my opinion, if port interfaces have method signatures that consume structs that separate layers depend on, then the layers are no longer loosely coupled which kind of defeats the purpose. What you decide to do really depends on the requirements of your application but a simple way to deal with it would be to avoid using structs between layers and instead make use of Go’s basic types.
Isnt there a mistake?
Database should be on the framework layer. App uses its port and violates the dependency direction principle.
I’ve never heard of the dependency direction principle but I think you should watch the part where I explain how we’re using dependency injection to ensure that the dependencies point inward.
Also, you should rewatch the part that explains how we’re using interfaces (abstractions) to expose the behavior needed between layers.
@@kantancoding It was a typo, I meant dependency inversion principle. And yes, I understand now. We define the interfaces at the lower level. The lower level defines the way how the outer details should behave. I think, this is a good way.
why does your vim have an extra UI/Command line?
INSERT
-- INSERT --
I have a video that explains my vim setup here: ua-cam.com/video/mbSaK3EOqO8/v-deo.html
please start data structures and algorithms course
Hello! Have you checked out my series that teaches data structures through building an API using Flask and Python? ua-cam.com/play/PL7g1jYj15RUP_Mri9ym6BdUais6_jvhrS.html
@@kantancoding yes i checked but please upload more videos if you have time.
the content is really nice, but can you please remake the tutorial if possible, as it would be a hassle for watching update video and understanding the same.
Thank you! Sorry, but I am going to have to say no to this request.
Heh! Talking like a bot!
The title is "how to structure". The video is "how to code". Sigh!
😂🤯