This Will Make Everyone Understand Golang Interfaces

  Рет қаралды 58,281

Anthony GG

Anthony GG

Күн бұрын

Пікірлер: 160
@anthonygg_
@anthonygg_ Жыл бұрын
► 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
@linuxshell8804
@linuxshell8804 10 ай бұрын
Johnny Sins is truly a modern polymath!
@tnypxl
@tnypxl Жыл бұрын
Interfaces are the only thing I struggle with in Golang (well, that and channels/concurrency)
@fullstackwithsantosh
@fullstackwithsantosh 8 ай бұрын
Relatable.
@earnstein7607
@earnstein7607 8 ай бұрын
How are you doing now? I'm just getting started
@crade47
@crade47 Жыл бұрын
The last part of the video where you added Name() was so important to understand and grasp the "contract" analogy fully, good vid
@anibaldk
@anibaldk Жыл бұрын
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.
@justanaveragebalkan
@justanaveragebalkan 9 ай бұрын
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?
@yugioh8810
@yugioh8810 7 ай бұрын
yes this is the biggest selling point of go interfaces, the fact that it's implicitly attached to structs by virtue of structural typing. I feel like this video is lacking the core idea behind interfaces in golang and is just showing the basic "what is an interface" in any other language.
@aleksajovanovic4900
@aleksajovanovic4900 5 ай бұрын
@@yugioh8810 give us a link to a more in depth video please
@PrabeshSharma-ed5pl
@PrabeshSharma-ed5pl 4 ай бұрын
the crazy thing is that you are every python dev discovering a statically typed lang
@rosehogenson1398
@rosehogenson1398 Жыл бұрын
#1 rule: don't define an interface until you have more than one implementation
@anthonygg_
@anthonygg_ Жыл бұрын
Yeah I like that thinking
@salman0ansari
@salman0ansari Жыл бұрын
Why?
@gearboxworks
@gearboxworks Жыл бұрын
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 Жыл бұрын
Rule 1: Plan and define the abstract first before jumping into the coding part.
@rosehogenson1398
@rosehogenson1398 Жыл бұрын
@@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"
@MrAverageViewer
@MrAverageViewer Жыл бұрын
THANK YOU again, Anthony, for demystifying complex concepts, with easy-to-understand real-world analogies most people can relate to!
@jonathanlonnfors3220
@jonathanlonnfors3220 6 ай бұрын
Football scouts taking notes on those crazy algorithms
@Fep_Aguilar
@Fep_Aguilar 11 ай бұрын
I'm just discovering this channel and I got suscribed just right now! Good job my man
@anthonygg_
@anthonygg_ 11 ай бұрын
❤️
@Furieshi
@Furieshi 4 ай бұрын
I learned that Ronaldo hits really hard. Thanks Zidane!
@spiraldynamics6008
@spiraldynamics6008 Ай бұрын
😂
@nanonkay5669
@nanonkay5669 Жыл бұрын
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.
@liftingisfun2350
@liftingisfun2350 3 ай бұрын
Actually, a struct only needs to have a subset of the functions on an interface to satisfy it. Try it yourself
@vilenczi
@vilenczi 8 ай бұрын
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. :)
@LeonardoBaracat
@LeonardoBaracat 5 ай бұрын
So then, the actual structs aren't inside of [ ]Player. Instead, their addresses. Is that correct?
@NORD_LK
@NORD_LK Жыл бұрын
This clears things up for interfaces with GO. Keep up the good work !
@ade_niko
@ade_niko Жыл бұрын
Always thought Arjen Robben was a Rust guy
@anthonygg_
@anthonygg_ Жыл бұрын
😂
@MykMallett
@MykMallett Жыл бұрын
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.
@GOTHICforLIFE1
@GOTHICforLIFE1 Жыл бұрын
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_ Жыл бұрын
Good idea . I have some more complex interface vids. I thought to keep it super simple for this one.
@perc-ai
@perc-ai Жыл бұрын
@@anthonygg_nah dude we need more complex ones show us actual software engineering strategies / implementation
@loverboykimi
@loverboykimi 29 күн бұрын
Best video on Interface// Thanks.
@susybaka8201
@susybaka8201 3 ай бұрын
Thanks man, you are an excellent teacher !!!!
@AbdulWahab-ev2ct
@AbdulWahab-ev2ct 7 ай бұрын
This is gold. very very well explained. Thanks for your work Anthony!
@MCDyma
@MCDyma Жыл бұрын
This video was included in the recommendation of Go Weekly
@rockkley9159
@rockkley9159 Жыл бұрын
Just in time when I started to face the problem of understanding interfaces in go. Thanks, you really helped me
@TehKarmalizer
@TehKarmalizer Жыл бұрын
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.
@teprox7690
@teprox7690 5 ай бұрын
4:53 If you only have 11 players, nobody can sit on the bench.
@ben.thornhill
@ben.thornhill Жыл бұрын
If I ever find myself in Belgium, I'm gonna reach out to meet up. You're the man.
@IAmWhil
@IAmWhil 6 ай бұрын
This was fantastic. Could you take it a step further and implement a factory pattern in front of this that spits out different types of players that all implement the kickball interface? Would be cool to see this go to the next logical place. This was so much better than the circle/rectangle interface explanation everyone seems to be giving.
@spiraldynamics6008
@spiraldynamics6008 Ай бұрын
It would be better if you give an exemple of a function "func Match ( p Player) {.....}" You can call Match(CR7) , Match(Messi) etc... it works
@polymath-403
@polymath-403 3 ай бұрын
which theme do you use in you vscode sir ?
@notcountdankula
@notcountdankula 3 ай бұрын
What theme are you using?
@IzanakiTheCreator
@IzanakiTheCreator Жыл бұрын
what's Anthony's vs code theme?
@anthonygg_
@anthonygg_ Жыл бұрын
Gruvbox
@sigfaults
@sigfaults 10 ай бұрын
This is a great video, good job!
@jac556
@jac556 5 ай бұрын
I love golang and I thought I got interfaces until I saw one in a struct lol Thanks for the video man
@SiCrip09
@SiCrip09 10 ай бұрын
Thanks for this! You gave me that big AHA and thumbs up for using VIM 🎉
@SakenBerdibekov
@SakenBerdibekov Жыл бұрын
Hi Anthony, how do you use rand without seeding?
@TN-kg2lv
@TN-kg2lv 24 күн бұрын
btw the vscode theme is Gruvbox
@RooterDelWifiXs
@RooterDelWifiXs 11 ай бұрын
What is your vscode theme?
@x0z59
@x0z59 Жыл бұрын
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!
@manankoyawala5337
@manankoyawala5337 11 ай бұрын
This example is clears my entire doubt about the interface in golang. Thank you ❤
@vincenthou6459
@vincenthou6459 11 ай бұрын
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.
@dazealex
@dazealex Жыл бұрын
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?
@mohamadbt4055
@mohamadbt4055 Жыл бұрын
it definitely helped me out to understand interface better thanks a lot
@___DS___
@___DS___ Жыл бұрын
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.
@seidigapbar
@seidigapbar Жыл бұрын
Hi Anthony, hilarious thumbnail, made me chuckle! Love your content :)
@anthonygg_
@anthonygg_ Жыл бұрын
Haha thx
@taylorjohnsonct
@taylorjohnsonct 2 ай бұрын
I love the honesty on the price of the course :D
@ForeverZer0
@ForeverZer0 Жыл бұрын
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.
@brieuclecarluer2769
@brieuclecarluer2769 Жыл бұрын
best Golang teacher ever
@ward7576
@ward7576 2 ай бұрын
the feeling I get listening to you explain Go interfaces must be the same that people getting a stroke feels - most warped explanation possible, can't even follow half the words, bruv.
@pythonantole9892
@pythonantole9892 Жыл бұрын
What theme are you using on VSCode. Looks very sleek!
@anthonygg_
@anthonygg_ Жыл бұрын
Gruvbox
@mareksvitok8702
@mareksvitok8702 Жыл бұрын
Hi Anthony, I'd like to ask you, why did you move to vscode from nvim? Just curious.
@abhinayshukla2215
@abhinayshukla2215 Жыл бұрын
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
@sanjayshiradwade
@sanjayshiradwade Жыл бұрын
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_ Жыл бұрын
It becomes messi (er)
@sanjayshiradwade
@sanjayshiradwade Жыл бұрын
@@anthonygg_ 🤣 right, Go-God
@rexxDigital
@rexxDigital Жыл бұрын
This made me understand golang interfaces! +1
@homelessrobot
@homelessrobot Жыл бұрын
you had no choice in the matter...
@jam_4769
@jam_4769 Жыл бұрын
Love your videos man. Thank you for making the best videos on golang
@realohh
@realohh Ай бұрын
You are doing a great job. Love your videos, they are really informative. tried joining your discord channel but I am being thrown an error.
@salman0ansari
@salman0ansari Жыл бұрын
can you make a video with practical use of interfaces?
@axMf3qTI
@axMf3qTI 11 ай бұрын
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?
@virium.8372
@virium.8372 Жыл бұрын
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
@First_Lst
@First_Lst Жыл бұрын
Great video!
@AndrewRomano-k6h
@AndrewRomano-k6h 9 ай бұрын
Great video.
@георгийелисеев-г9х
@георгийелисеев-г9х 8 ай бұрын
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?)
@alexandrodisla6285
@alexandrodisla6285 Жыл бұрын
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.
@lucy-pero
@lucy-pero Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
@@rosehogenson1398 i see. thank you!
@leopet6815
@leopet6815 Жыл бұрын
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
@damm_coder4550
@damm_coder4550 11 ай бұрын
😂😂Got to learn with comedy characters! CR7 and Suuuii Fked Little less messi 🤣 15:41
@abhinayshukla2215
@abhinayshukla2215 Жыл бұрын
Weather it is handler function or webserver or service layer or repo layer mocking everything
@adilfarq3784
@adilfarq3784 Жыл бұрын
U are my best teacher 😊
@albertoguzman9390
@albertoguzman9390 Жыл бұрын
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?
@dukeofnorfolk1842
@dukeofnorfolk1842 Жыл бұрын
You know the "append()" function exist 😂
@abdulrahmanmohamed8298
@abdulrahmanmohamed8298 Жыл бұрын
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_ Жыл бұрын
Im using gruvbox. The hard contrast version. 😼
@androth1502
@androth1502 10 ай бұрын
it doesn't seem easily scalable. add one function to the interface and now you have to make potentially dozens of changes.
@md.saifulislam6528
@md.saifulislam6528 11 ай бұрын
Good one
@SaiyanJin85
@SaiyanJin85 3 ай бұрын
omg, you do look like Roben, what a great player ;)
@FekuEntertainmentLtd
@FekuEntertainmentLtd 10 ай бұрын
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.
@LarsKniep
@LarsKniep Жыл бұрын
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 ;-)
@mohamedhabas7391
@mohamedhabas7391 4 ай бұрын
thank you
@kamilziemian995
@kamilziemian995 11 ай бұрын
Good tutorial.
@vanvanni_nl
@vanvanni_nl 11 ай бұрын
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
@anthonygg_
@anthonygg_ 11 ай бұрын
@TheMouseJerry-du1md
@TheMouseJerry-du1md 8 ай бұрын
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
@crikxouba
@crikxouba 6 ай бұрын
I appreciate these free videos, I just wish they had less waffle and were more straight to the point. I feel this video could have been 5 minutes.
@deniyii
@deniyii Жыл бұрын
😂😂😂😂 The ronaldo love was too funny
@BinaryMaestro1
@BinaryMaestro1 10 ай бұрын
thanks
@manishbadgotra
@manishbadgotra Жыл бұрын
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?
@sagimor8646
@sagimor8646 Жыл бұрын
Thank you Anthony, great video but for some reason every time CR7 getting higher score than Messi Go seems to Panic.
@anthonygg_
@anthonygg_ Жыл бұрын
Haha good one.
@manfrombritain6816
@manfrombritain6816 11 ай бұрын
mutexes locking, DELTS POPPING
@rubyciide5542
@rubyciide5542 29 күн бұрын
Anthony Robben
@patrickkdev
@patrickkdev 7 ай бұрын
I love your videos but I really want to leverage interfaces and I don't think this example was very good. In this cenario, it would be simpler to just have the name and sui factor fields in the original FootballPlayer struct and set them to default values ("" and 0) for regular players rather then creating new structures for special players Edit: The example was good for understanding how they work, but not for learning how to leverage them I know how to use interfaces but it is hard for me to think of solutions using them rather then something else
@anthonygg_
@anthonygg_ 7 ай бұрын
You might have a point here.
@AlejandroDavidPoirierGomez
@AlejandroDavidPoirierGomez Жыл бұрын
Love your channel, you know what i mean?
@anthonygg_
@anthonygg_ Жыл бұрын
Thanks man. Yeah I know 😂
@zbaktube
@zbaktube Жыл бұрын
In soccer if you have only 11 players, even the ones with shot 1 will stay off the bench for the whole season ;-) 🙂
@oliverberning130
@oliverberning130 3 ай бұрын
It is simply not possible to explain it in a simpler way - for people coming from another language at least.
@alexbork4250
@alexbork4250 6 ай бұрын
damn, I thought Rust fixed it. But I'm wrong, I have to learn this "language" because it has ten times more vacancies
@fakedevdutt
@fakedevdutt 5 ай бұрын
IDK if he taught the interface but talk about ronaldo! he made that clear
@konsumgandalf8006
@konsumgandalf8006 Жыл бұрын
U should train more
@anthonygg_
@anthonygg_ Жыл бұрын
I agree.
@fakedevdutt
@fakedevdutt 5 ай бұрын
bro thinks he is "Carl Johnson"
@demyk214
@demyk214 Жыл бұрын
Voetbal😂 i catch u slacking again brodie
@anthonygg_
@anthonygg_ Жыл бұрын
Fuck 😂
@gocanto
@gocanto Жыл бұрын
Course is still pricy :/
@kevinb1594
@kevinb1594 Жыл бұрын
Do CONTEXT next
@ronindevninja
@ronindevninja 6 ай бұрын
I love it
@abhinayshukla2215
@abhinayshukla2215 Жыл бұрын
Please make a full fledge unit test , integration test on all possible scenarios
@as27
@as27 Жыл бұрын
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_ Жыл бұрын
Ok buddy
@andreas3858a
@andreas3858a Ай бұрын
People on Java / Any proper/general OOP: wtf this sh*t. make life so hard The same people on broken Go's Interfaces: this is awesome. make life so easy. If you don't want OOP, it's fine. But don't force it half-baked. having interface without class are so stupid.
What Is THE BEST Web Framework In Golang? Why?
14:39
Anthony GG
Рет қаралды 50 М.
Go Pointers: When & How To Use Them Efficiently
14:09
Anthony GG
Рет қаралды 91 М.
小丑女COCO的审判。#天使 #小丑 #超人不会飞
00:53
超人不会飞
Рет қаралды 16 МЛН
How Strong Is Tape?
00:24
Stokes Twins
Рет қаралды 96 МЛН
Каха и дочка
00:28
К-Media
Рет қаралды 3,4 МЛН
Master Golang with Interfaces
21:54
Kantan Coding
Рет қаралды 20 М.
Beginners Should Think Differently When Writing Golang
11:35
Anthony GG
Рет қаралды 133 М.
Go Iterators Are Bad
36:12
ThePrimeTime
Рет қаралды 109 М.
How To Use The Context Package In Golang?
17:03
Anthony GG
Рет қаралды 69 М.
A Practical Example How To Use Interfaces In Golang
14:42
Anthony GG
Рет қаралды 31 М.
Golang: The Last Interface Explanation You'll Ever Need
17:58
Flo Woelki
Рет қаралды 29 М.
3 Golang Tips For Beginners I Wish I Knew Sooner
13:18
Anthony GG
Рет қаралды 52 М.
Creating custom struct tags in Golang is awesome!
24:37
Flo Woelki
Рет қаралды 11 М.
Advanced Golang: Channels, Context and Interfaces Explained
22:17
Code With Ryan
Рет қаралды 129 М.
小丑女COCO的审判。#天使 #小丑 #超人不会飞
00:53
超人不会飞
Рет қаралды 16 МЛН