How To Compose Interfaces In Golang?!
By Anthony GG
Summary
Topics Covered
- Part 1
- Part 2
- Part 3
- Part 4
- Part 5
Full Transcript
what is up everyone in this video I'm going to teach you everything you need to know about composability with interfaces in golang this is the breadth and build it of the language this is very important
um unfortunately it's not being teached so much on the internet and if it's being teached it's being teached badly I'm going to teach you how to use this for production environment for real life use cases and for the people that
are not subscribed yet please consider subscribing to my channel give me a thumbs up leave some questions in the comment and if you really want to jump into the Discord where we have 24 7
education where I'm turning Engineers into high value software Engineers let's get into it so basically uh what's going on it I have two screens open let's
close this one real quick so I'm going to have this use case right and um we have hash and broadcast which will take in an IO reader right
it's like I said before I'm closing the gap between simple tutorial code and real life use cases so you don't choke in your job
so it's a very common use case where you have an i o reader right and uh what you're going to do here is basically we're going to take the hash right we're going to take the hash of a read it which is basically uh the hash
of the bytes which are being which we can read from the reader right so how a lot of people are going to do this is basically the wrong way right so they're gonna say B Ash is going to be uh Iota
deal right they use Iota and gonna say read all right read all the passing the reader here we're gonna say if the error is not nil they will um just return the error here
which is fine right then we have the bytes heat and then you can see that the hash is basically um for example Xiao one some of the bytes right
like this and then we can print out fmt rental and we're going to say hex and codes to string and we're gonna say um the hash right it's not going to work
because this is going to be I think a 10 let me see what some is doing it Returns the size which should be 20 I think not quite sure so and to be able to fix this we just do this and we have a nice slice
of bytes right so we're gonna read everything from our readers we're gonna hash it and then we're gonna print out a nice string representation of the hash and then we are going to pipe in the
reader back to broadcast right and we are going to do the same thing in broadcast because what we're going to do in broadcast I don't know probably broadcasting it is just for the sake of demonstration so we're gonna Retreat it once again hit and then we're gonna say
mmt rental uh let's say string of the re of the bytes for example and then do this and say that's
a string of B right and then in our main function we could say for example that the payload is going to be uh bytes and wait we could do it like
this for example just a simple byte I'm gonna say um hello high value software engineer
like this and then we're going to say hash and broadcast and of course we cannot just put in the build because it's a byte so we're going to say a byte we need to leave it so I'm going to say bytes
like this and put in the payload like that and then we're gonna say uh go run may not go like this boom and
you can see that we have a nice hash but the string of the bytes is empty and that's a problem and now you're gonna spend the whole week trying to solve this and if you're solving it you're
probably going to solve it in a bad way uh and the reason is because we have a reader here right we're gonna read everything from the reader so that basically means that once we pass the reader and broadcast right here
we cannot read anymore because the reader is already being read it's completely empty there is no water anymore and they can you know what I mean so how are you going to solve that
is um I'm going to teach you how to solve it with interface composability right so what we could do for example is we could make a type and that's going to be a
hash read it a hash reader which is going to be a strict like this right and it's going to take in
um it's going to take any actually we could do it like this we can end that the io read it right this is an interface we can embed that into our strict
and then we're going to say uh actually not we're gonna not not do an iot if you're going to say a byte City like that uh and then we're gonna say
um we we gonna say it takes in a buffer which is going to be appointed to a bytes uh buffer like that right then
we're going to say funk new hash read it like this hash reader that's going to take in a bunch of bytes like this and it's going to return as a
pointer to that hash read it like this right we're gonna return the hash reader like this then we're gonna say that the reader right here which is
basically uh this embedded reader right we're gonna say it's going to be a bytes new read it of B
what's going on uh I need to do it like this I guess that's fine then we're gonna say that we need a buffer and the buffer is
going to be a bytes new Buffet we're gonna put that into this like that and now we're going to say instead of basically
um let's give this a hash function and we also need to give it a read function but I think that's already going to be okay we're going to say because the read function has already been implemented
right because we and that bytes really the right heat so but we need a hash function which is going to be let's make it a small small case we're going to say hash it's going to return as a string right and there's going to be of course a funk
um let's call it this a hash readers like that and we're going to say here we're going to say return hex and code
to string and it's going to be the hey buff bytes like this and we are done so how are we going to fix this right so instead of saying that we're gonna buy
we're gonna do a bytes new reader what we're going to do is instead of a bison leader we're going to say a new hash read it with this payload right uh just like that
but the problem is um it's going to work all fine right because the hash reader the payload is gonna it's gonna work out fine heat and hasn't broadcast because like I mentioned before we are composing the
byte city which already implements the io needed interface into our hash reader so we don't need to do it it will just work out of the bed and you can make it
even more complex uh but hey this is good for now just an example on how to do these things so this hash so what you're going to do instead of reading this instead of
depleting what we're going to do is uh say that the hash actually what you could do um is saying that the hash is going to be R and we're going to cost this reader to a
hash reader like this and we're going to say we're going to call the function hash to it right it's going to return a hash and instead of this because it's a string so we don't need to do Hex and codes we can just
print this out and let's test it out boom as you can see we have a nice hash of our string and we have the
the bytes by just a simple uh golang interface composability right but now you could say yes yes Anthony good good but I don't like this hash reader thing he is
right because uh it's nasty and you're 100 right we're gonna fix that also by doing some uh interface composability once again so what we're going to say is Let's Make A type and we're going to
call this a hash reader and it's going to be an interface right and what do we need for the hash read it so very simple the the type itself the type name already says what it needs to do we're
going to say IO read it right we don't need to specify a read function we're just going to embed the i o reader interface right which is basically this is just read a bunch of bytes and it returns someone how many bytes has
written and an error right so we're going to embed this and then we're going to say yo we also want a hash function because it's a hash read it for example and we're just going to return a string of course in a real world scenarios you
are going to return bytes or something right I mean uh you could have a hash reader which could be you could implement this in a multiple things you could have a sha1 hash 3D equal to a
shot two five six hash feeder you could whatever hmac okay you name it you are the the painter of your painting your developers of programming so you can choose whatever you want
right don't listen to all these fake gurus come to my channel and I will teach you how to make things happen right I will turn you into a high value software engineer so um yes so instead of saying hit
this hasn't broadcast instead of taking a reader right we could say uh I want um a hash reader like this
what a hash reader and instead of uh calling this directly here we could say that the hash
is going to be or hash right you see it can read with r you can see it can it has a hash function and it has a read function on top of it so we're going to say hash
right and then we don't need to specify a hash leader hit in broadcast right you can just say I read it because uh in broadcast the only thing we're going to do is read from it we're not going to Hash it so we can just provide an i o
reader and that's why you have composability because for testing this this broadcast function the only you only need to read but for testing this hash and broadcast
function you need a hash reader so it's it's very composable it's very testable maintainable uh it's basically everything you need to know it's very very important uh and let's run it again
go run main.go you see and it works perfectly fine so we have a nice way to compose interfaces uh even instructs and interfaces itself to basically make a
program much more easier to maintain to understand and to switch things around to compose right that's the future of programming much better than all these
object oriented right so if you like this video consider subscribing to my channel because I'm gonna drop more of these insane videos which will
make every interview which make which will make every interviewer just sit down because you are the man if you know this right I also have a
patreon page where I'm doing uh the craziest ever I'm making a completely decentralized content addressable store where I'm gonna teach you everything about uh multi multiplexing TCP streams and all that
stuff and how to distribute and decentralize uh things so if you want take a look at my patreon page and thanks for watching this video and I'll see you in one of my live streams bye bye
Loading video analysis...