This Will Make Everyone Understand Golang Interfaces

  Переглядів 38,140

Anthony GG

Anthony GG

7 місяців тому

► Join my Discord community for free education 👉 / discord
► Exclusive Lessons, Mentorship, And Videos 👉 / anthonygg_
► Enjoy a 50% Discount on My Golang Course 👉 fulltimegodev.com
► Learn how I became a self-taught software engineer 👉fulltimegodev.com/#mystory
► Follow me on Twitter 👉 / anthdm
► Follow me on GitHub 👉 github.com/anthdm
SUBSCRIBE OR NO MARGARITAS
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝

КОМЕНТАРІ: 121
@anthonygg_
@anthonygg_ 7 місяців тому
► Join my Discord community for free education 👉 discord.com/invite/Ac7CWREe58 ► Exclusive Lessons, Mentorship, And Videos 👉 www.patreon.com/anthonygg_ ► 50% OFF on my Golang course 👉 fulltimegodev.com Thanks for watching
@rosehogenson1398
@rosehogenson1398 7 місяців тому
#1 rule: don't define an interface until you have more than one implementation
@anthonygg_
@anthonygg_ 7 місяців тому
Yeah I like that thinking
@salman0ansari
@salman0ansari 7 місяців тому
Why?
@gearboxworks
@gearboxworks 7 місяців тому
Hmmm, not exactly. As one use-case where that would not necessarily be true would be when using an interface to resolve cyclic dependencies across packages. Another use-case is when *you* have only one implementation, but you want to enable others to but able to pass in their implementations. In a nutshell, interfaces in Go are very powerful and there are more than just one want to leverage them.
@livingintheabstraction
@livingintheabstraction 7 місяців тому
Rule 1: Plan and define the abstract first before jumping into the coding part.
@rosehogenson1398
@rosehogenson1398 7 місяців тому
@@salman0ansari Having more than one implementation allows you to see clearly where the common behavior is. If you define an interface with only one implementation, first of all it's usually unnecessary, but also it's too tempting to put every single one of the implementation struct's methods into the interface. That makes it hard to add any new implementations, because they have to define every single method that the original struct did, even if not all of them are relevant for the new implementation. Waiting to define interfaces avoids over-abstracting, and helps to keep your interfaces minimal and focused. "The bigger the interface, the weaker the abstraction"
@linuxshell8804
@linuxshell8804 Місяць тому
Johnny Sins is truly a modern polymath!
@anibaldk
@anibaldk 3 місяці тому
Something to point out - a BIG difference from other programming languages - is that the type (in this case CR7, Messi or FootballPlayer) does not explicitly implement the interface as it would for example, in C# or Java where the class declares implementing it. Instead, it is enforced by the Go compiler which checks the function to be declared for the type.
@kristiformilchev6417
@kristiformilchev6417 Місяць тому
How would that work in the case of the following INewsService -> Get() [] News and ICacheRepository -> Get() []News. How does that cultist dark art work in that given scenario, would the compiler be like, no you stupid human, you can't declare a method Get twice because it's already used somewhere else or is it going to be fine, you know what you're doing and execute both the Cache and NewService implementation each time we call it either ICacheRepository or INewService?
@MrAverageViewer
@MrAverageViewer 7 місяців тому
THANK YOU again, Anthony, for demystifying complex concepts, with easy-to-understand real-world analogies most people can relate to!
@crade47
@crade47 7 місяців тому
The last part of the video where you added Name() was so important to understand and grasp the "contract" analogy fully, good vid
@vilenczi
@vilenczi 6 днів тому
After 7+ years of senior python thinking for me this was the 'AHA' moment right here: " team := make([]Player, 11) for i := 0; i < (len(team) - 2); i++ " I was like 'what???, you can loop over a slice of "capabilites", not just actual objects???" I think this opens a whole range of different design ideas, way of decouplig, testing, etc. Cos you're focusing on the capability, not the object / instance or whatever. Now the term 'contract' makes a lot more sense. Because you are saying, hey I don't care WHAT you are as long as you CAN DO this or that. BTW, thanks a lot Anthony! In addition to the depth of your (applicable real life) knowldge I really like your style. Feels likehaving a coding conversation in pub w. a beer. :)
@ranilmadawalage1897
@ranilmadawalage1897 7 місяців тому
This clears things up for interfaces with GO. Keep up the good work !
@Fep_Aguilar
@Fep_Aguilar 3 місяці тому
I'm just discovering this channel and I got suscribed just right now! Good job my man
@anthonygg_
@anthonygg_ 3 місяці тому
❤️
@rockkley9159
@rockkley9159 7 місяців тому
Just in time when I started to face the problem of understanding interfaces in go. Thanks, you really helped me
@nanonkay5669
@nanonkay5669 7 місяців тому
How I think of an interface is that it is a struct but for functions. So while a struct describes what properties an "object" has, and an interface defines what functions an "object" has, though not only limited to those functions. So as long as your object has all the functions in the interface, it implements that interface. Meaning if you have a function or method that only works at an interface level, that "object" is a valid candidate.
@tnypxl
@tnypxl 7 місяців тому
Interfaces are the only thing I struggle with in Golang (well, that and channels/concurrency)
@fullstackwithsantosh
@fullstackwithsantosh 10 днів тому
Relatable.
@earnstein7607
@earnstein7607 9 днів тому
How are you doing now? I'm just getting started
@mohamadbt4055
@mohamadbt4055 7 місяців тому
it definitely helped me out to understand interface better thanks a lot
@x0z59
@x0z59 7 місяців тому
You're the most enthusiastic and incredible! Well you got it, INCREDIBLE golang guru I have ever watched in youtube! you deserve millions of subs, bro!
@Jam-ht2ky
@Jam-ht2ky 7 місяців тому
Love your videos man. Thank you for making the best videos on golang
@MykMallett
@MykMallett 7 місяців тому
I find that the biggest problems with interfaces in Go is that people have pre existing ideas for how they should be designed, ie defining the interface first with the implementation. In Go you don't do this, you define an interface where you want to use it. Because they are implicit rather than explicit it's almost a complete reversal of thinking, and it catches a lot of people out.
@SiCrip09
@SiCrip09 Місяць тому
Thanks for this! You gave me that big AHA and thumbs up for using VIM 🎉
@azuremagic
@azuremagic 7 місяців тому
If I ever find myself in Belgium, I'm gonna reach out to meet up. You're the man.
@___DS___
@___DS___ 7 місяців тому
Hello, mate I extremely love ur content. I was curious about ur course (despite the price), so had a look into it. As a dev with 7 months of commercial exp in go (microservice architecture) (2,5 years overall) I can tell ur course (imho) lacking very important aspect (from the glance to the full course description) - and it's work with DB. Especially, when u need to do bunch of aggregations, hit the endpoint from one service to another, sending bunch of some data to sync and upsert relative data in the db of the other microservice, etc. etc. Mb I'm wrong and didn't notice that in the list, but without this I reckon course isn't complete at all. Anyways, thanks for what u r doing.
@mareksvitok8702
@mareksvitok8702 7 місяців тому
Hi Anthony, I'd like to ask you, why did you move to vscode from nvim? Just curious.
@GOTHICforLIFE1
@GOTHICforLIFE1 7 місяців тому
I think the explanation is decent, but i do also think it would be beneficial to have a followup where you truly see the benefits of interfaces. Maybe in combination with a strategy pattern where it's commonly used. Strategy patterns are pretty mandatory to understand as a professional developer imo. It's where i learned to understand interfaces, as it's a perfect place to use it.
@anthonygg_
@anthonygg_ 7 місяців тому
Good idea . I have some more complex interface vids. I thought to keep it super simple for this one.
@perc-ai
@perc-ai 7 місяців тому
@@anthonygg_nah dude we need more complex ones show us actual software engineering strategies / implementation
@seidigapbar
@seidigapbar 7 місяців тому
Hi Anthony, hilarious thumbnail, made me chuckle! Love your content :)
@anthonygg_
@anthonygg_ 7 місяців тому
Haha thx
@eldr-io
@eldr-io 2 місяці тому
This is a great video, good job!
@manankoyawala5337
@manankoyawala5337 2 місяці тому
This example is clears my entire doubt about the interface in golang. Thank you ❤
@SakenBerdibekov
@SakenBerdibekov 7 місяців тому
Hi Anthony, how do you use rand without seeding?
@First_Lst
@First_Lst 7 місяців тому
Great video!
@ForeverZer0
@ForeverZer0 7 місяців тому
I personally think the biggest "gotcha" with learning interfaces coming from different languages is the dual behavior they have: either as a type constraint, or the traditional "thing that implements", but not both simultaneously. You can use an interface as a field in struct if it implements functions, but if you change it to a type constraint, it no longer works. When I was initially learning Go, this was unexpected to me.
@virium.8372
@virium.8372 7 місяців тому
do you think you could make a video on tui on a telnet/ssh server? im currently trying to make a casino for fun so me and my friends can play but i cant seem to make any good tui for instance i cant get it to work on the ssh server
@MCDyma
@MCDyma 7 місяців тому
This video was included in the recommendation of Go Weekly
@pythonantole9892
@pythonantole9892 7 місяців тому
What theme are you using on VSCode. Looks very sleek!
@anthonygg_
@anthonygg_ 7 місяців тому
Gruvbox
@rexxDigital
@rexxDigital 7 місяців тому
This made me understand golang interfaces! +1
@homelessrobot
@homelessrobot 7 місяців тому
you had no choice in the matter...
@abdulrahmanmohamed8298
@abdulrahmanmohamed8298 7 місяців тому
Ive been binging many of your Golang vids and find em really helpful. Thanks for your tips and insight Anthony. btw anyone know what vscode theme he's using?
@anthonygg_
@anthonygg_ 7 місяців тому
Im using gruvbox. The hard contrast version. 😼
@leopet6815
@leopet6815 4 місяці тому
Thank you! That did help a lot. I was wondering if I could do something better because I almost never declared my own interfaces. I actually calmed down a bit because a lot of my code is very interconnected with the database...and my logic used interfaces that were already made for me by the db engines and stuff
@adilfarq3784
@adilfarq3784 7 місяців тому
U are my best teacher 😊
@TehKarmalizer
@TehKarmalizer 3 місяці тому
I think the confusion primarily comes from Go interfaces being more descriptive than prescriptive. You don’t declare you are implementing an interface for a type, so there is no checking whether a type actually implements it until you try to use it as interpreted by the interface.
@dazealex
@dazealex 4 місяці тому
Are you using some kind of Vim plugin for VSC? You're super fast with. Any hints on getting that good at text/code editing?
@salman0ansari
@salman0ansari 7 місяців тому
can you make a video with practical use of interfaces?
@abhinayshukla2215
@abhinayshukla2215 6 місяців тому
Anthony I have a question if I made a receiver function on a struct which is global access but my struct is private so how do I unit test that
@user-sb9nt9ho6d
@user-sb9nt9ho6d Місяць тому
Great video.
@md.saifulislam6528
@md.saifulislam6528 3 місяці тому
Good one
@sanjayshiradwade
@sanjayshiradwade 7 місяців тому
This is how it looks without interfaces, As you'll see, it becomes messier and less scalable. package main import ( "fmt" "math/rand" ) type Rolando struct { name string stamina int power int sPower int } type Messi struct { name string stamina int power int sPower int } type FootbalPlayer struct { name string stamina int power int } func KickBallExample(player interface{}) { switch p := player.(type) { case Rolando: shot := p.stamina + p.power + p.sPower fmt.Printf("%sshot: %d ", p. name, shot) case Messi: shot := p.stamina + p.power + p.sPower fmt.Printf("%sshot: %d ", p. name, shot) case FootbalPlayer: shot := p.stamina + p.power fmt.Printf("%sshot: %d ", p. name, shot) default: fmt.Println("Unknown player type") } } func main() { team := make([]interface{}, 11) for i := 0; i < len(team)-2; i++ { team[i] = FootbalPlayer{ stamina: rand.Intn(10), power: rand.Intn(10), name: "random", } } team[len(team)-2] = Rolando{ stamina: 10, power: 10, sPower: 10, name: "Ronaldo", } team[len(team)-1] = Messi{ stamina: 10, power: 10, sPower: 8, name: "Messi", } for i, player := range team { KickBallExample(player) } }
@anthonygg_
@anthonygg_ 7 місяців тому
It becomes messi (er)
@sanjayshiradwade
@sanjayshiradwade 7 місяців тому
@@anthonygg_ 🤣 right, Go-God
@user-nl2lk2js9p
@user-nl2lk2js9p 21 годину тому
In your other video you have said that we should not create a lot of files. But I think it would be perfect to place CR7, Messi and FootballPlayer into different files (if for instance they had larger logic inside). Isn't it?)
@brieuclecarluer2769
@brieuclecarluer2769 6 місяців тому
best Golang teacher ever
@vincenthou6459
@vincenthou6459 2 місяці тому
14:26 Roberto Carlos, 14:29 Arjen Robben. I am from the time of these people's. 13:48 Belgian goalie. I was there as well, coz I used to study in Belgium. This is a good video, catching my past.
@alexandrodisla6285
@alexandrodisla6285 7 місяців тому
Well I am from a third world country. The pricing of your course is basically more than half my salary. If it was on udemy at a low price I would buy it. Or if it was 15-30$ a month I would buy it. I love your style. thanks for the youtube videos.
@kamilziemian995
@kamilziemian995 3 місяці тому
Good tutorial.
@RooterDelWifiXs
@RooterDelWifiXs 3 місяці тому
What is your vscode theme?
@FekuEntertainmentLtd
@FekuEntertainmentLtd Місяць тому
Hey Anthony, I've recently been getting your video suggestions on YT since I just started with Golang. I don't if anybody has mentioned this to you, But in your video the voice is very low. Please check it out and thank you for the knowledge you share.
@albertoguzman9390
@albertoguzman9390 4 місяці тому
Hi guys! can someone explain to me why rand.Intn(10) return values greater than 10?, it should return a value between (0,n) but no greater than, right?
@BinaryMaestro1
@BinaryMaestro1 2 місяці тому
thanks
@IzanakiTheCreator
@IzanakiTheCreator 6 місяців тому
what's Anthony's vs code theme?
@anthonygg_
@anthonygg_ 6 місяців тому
Gruvbox
@deniyii
@deniyii 7 місяців тому
😂😂😂😂 The ronaldo love was too funny
@lucy-pero
@lucy-pero 7 місяців тому
sorry i don't code in Go but how can you allocate an Interface instead of a struct? how does it even know the size of what it is allocating? in order for this to work, Go is doing some wild magic behind the scenes. Maybe it allocates a base struct with virtual methods? This seems pretty bad for performance and other things.
@rosehogenson1398
@rosehogenson1398 7 місяців тому
Virtual methods is the right idea. An interface in go is represented as 2 words: a pointer to the virtual method lookup table, and a pointer to the data itself.
@lucy-pero
@lucy-pero 7 місяців тому
@@rosehogenson1398 i see. thank you!
@abhinayshukla2215
@abhinayshukla2215 6 місяців тому
Weather it is handler function or webserver or service layer or repo layer mocking everything
@zbaktube
@zbaktube 7 місяців тому
In soccer if you have only 11 players, even the ones with shot 1 will stay off the bench for the whole season ;-) 🙂
@vanvanni_nl
@vanvanni_nl 2 місяці тому
I just wonder, all the base calculations are the same. Power + Stamina. So can you abstract that to something and apply that to all. So kickBall = getBasePower on the FootballPlayer and on CR7 kickball = getBasePower * SUI
@TheMouseJerry-du1md
@TheMouseJerry-du1md 6 днів тому
thank u, but it would be more usuful if you can show how interfaces and structs present in 3rd party packages can be used in your own application and the patterns potentially be used
@ade_niko
@ade_niko 3 місяці тому
Always thought Arjen Robben was a Rust guy
@anthonygg_
@anthonygg_ 3 місяці тому
😂
@LarsKniep
@LarsKniep 7 місяців тому
You can even let your mom enter the football team as long as she can 'KickBall()' She isnt even a football player, but it doesn't matter ;-)
@manfrombritain6816
@manfrombritain6816 2 місяці тому
mutexes locking, DELTS POPPING
@anthonygg_
@anthonygg_ 3 місяці тому
@sagimor8646
@sagimor8646 5 місяців тому
Thank you Anthony, great video but for some reason every time CR7 getting higher score than Messi Go seems to Panic.
@anthonygg_
@anthonygg_ 5 місяців тому
Haha good one.
@dukeofnorfolk1842
@dukeofnorfolk1842 4 місяці тому
You know the "append()" function exist 😂
@axMf3qTI
@axMf3qTI 2 місяці тому
So we make a slice with a type Player and populate that slice with FootballPlayers and nowhere else we tell the program that football players are indeed players? Snap jij het nog?
@as27
@as27 7 місяців тому
First thank you for your passion it is great to show how much fun developing is. But your code is not a good example. If you have a slice you should use range. To use for, looks a little bit like old JavaScript to me. Also the explanation of the interface is how interfaces works in general. It is a good explanation, but it does not cover the Go specific approach. The goal of defining an interface is that the consumer can define the interface. If you have a function "kickIt(players []player)" the kickIt-package can define the interface. The packages "basketball", "americanfootball", "soccer" even don't need to know that there is an interface. There is not "type a implements interface player" in Go. This makes the packages more flexible.This is the idea of interfaces in Go.
@anthonygg_
@anthonygg_ 7 місяців тому
Ok buddy
@androth1502
@androth1502 Місяць тому
it doesn't seem easily scalable. add one function to the interface and now you have to make potentially dozens of changes.
@damm_coder4550
@damm_coder4550 2 місяці тому
😂😂Got to learn with comedy characters! CR7 and Suuuii Fked Little less messi 🤣 15:41
@demyk214
@demyk214 7 місяців тому
Voetbal😂 i catch u slacking again brodie
@anthonygg_
@anthonygg_ 7 місяців тому
Fuck 😂
@manishbadgotra
@manishbadgotra 5 місяців тому
Can we Indian’s have a discount over this 250 dollar price, because of Purchase power parity this amount is little expensive for students to buy?
@user-zz3tz5fi1b
@user-zz3tz5fi1b 5 місяців тому
Love your channel, you know what i mean?
@anthonygg_
@anthonygg_ 5 місяців тому
Thanks man. Yeah I know 😂
@kevinb1594
@kevinb1594 7 місяців тому
Do CONTEXT next
@user-zq8bt6hv9k
@user-zq8bt6hv9k 7 місяців тому
Next video, explain crazy function with interface interface * wtf
@user-zq8bt6hv9k
@user-zq8bt6hv9k 7 місяців тому
As parameters
@vitorvaz6281
@vitorvaz6281 3 дні тому
Thank you johnny sins XD
@konsumgandalf8006
@konsumgandalf8006 3 місяці тому
U should train more
@anthonygg_
@anthonygg_ 3 місяці тому
I agree.
@anibaldk
@anibaldk 3 місяці тому
Explanation was spot on but you don't get my like for suggesting Ronaldo's shots were more powerful than Messi's. Just joking.
@AlgyTRG
@AlgyTRG 2 місяці тому
how is that Messi SUI, probably the greatest player in history, is lower than CR7? c'mon!
@gocanto
@gocanto 7 місяців тому
Course is still pricy :/
@abhinayshukla2215
@abhinayshukla2215 6 місяців тому
Please make a full fledge unit test , integration test on all possible scenarios
@jonathanlambrecht5642
@jonathanlambrecht5642 6 місяців тому
i found a big mistake in your video, Messi>ronaldo.
@birenbharat
@birenbharat 3 місяці тому
to much time for small concept
@indrranil24
@indrranil24 27 днів тому
suiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
@MENTOKz
@MENTOKz 7 місяців тому
i get it so cr7 is better than messi 😂
@serhioromano
@serhioromano 2 місяці тому
You are a smart guy and have great point in your videos. But the problem I have with it, after spearing more than 20 minutes snatching this I realized that you lost a lot of my time. You could explain this concept in less that 10 minutes without losing quality. Please make your videos shorter. This is why but the end of this video I lost interest to purchase you choose. If you talk so much empty words there I'll lose a lot of time. I do not want to pay someone to entertain me. I need knowledge. And I'm an sorry, no one will tell you that nowadays, but you are not that funny, but you are defensively very experienced developer. This is you doing past. Lean on it.
@victorburca5028
@victorburca5028 17 днів тому
bla-bla-bla... I am here to learn how to code GO, not to listen to a parrot.
@anthonygg_
@anthonygg_ 17 днів тому
Shut up Timmy and bring me my coffee.
@kiritocyanpine5018
@kiritocyanpine5018 7 місяців тому
The interface paradox I face when developing a feature, "when to implement an interface?" ... 🫥
What Is THE BEST Web Framework In Golang? Why?
14:39
Anthony GG
Переглядів 33 тис.
Go Pointers: When & How To Use Them Efficiently
14:09
Anthony GG
Переглядів 60 тис.
Эффект Карбонаро и пончики
01:01
История одного вокалиста
Переглядів 3,9 млн
skibidi toilet 73 (part 2)
04:15
DaFuq!?Boom!
Переглядів 24 млн
A Practical Example How To Use Interfaces In Golang
14:42
Anthony GG
Переглядів 15 тис.
The Power Of Struct Embedding And Interfaces In Golang
15:05
Anthony GG
Переглядів 12 тис.
Beginners Should Think Differently When Writing Golang
11:35
Anthony GG
Переглядів 85 тис.
Start using this Go design pattern.. Consumer Interfaces!
6:05
eldr-io
Переглядів 4,1 тис.
Why I Use Golang In 2024
9:21
ThePrimeTime
Переглядів 214 тис.
19. Rob Pike - What We Got Right, What We Got Wrong | GopherConAU 2023
47:21
Why #Go Interfaces? / #Golang Interfaces Tutorial
9:41
defer panic
Переглядів 29 тис.
Advanced Golang: Channels, Context and Interfaces Explained
22:17
Code With Ryan
Переглядів 104 тис.
How To Setup A Golang + Templ Project Structure
31:20
Anthony GG
Переглядів 41 тис.
How Does A Typical Day As An ASYNC Developer Look Like?
13:04
Anthony GG
Переглядів 6 тис.