Interfaces continued

GetGoing: Introduction to Golang Data types and control structure
4 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
This is a free item
$0.00
د.إ0.00
Kz0.00
ARS$0.00
A$0.00
৳0.00
Лв0.00
Bs0.00
B$0.00
P0.00
CA$0.00
CHF 0.00
CLP$0.00
CN¥0.00
COP$0.00
₡0.00
Kč0.00
DKK kr0.00
RD$0.00
DA0.00
E£0.00
ብር0.00
€0.00
FJ$0.00
£0.00
Q0.00
GY$0.00
HK$0.00
L0.00
Ft0.00
₪0.00
₹0.00
ISK kr0.00
¥0.00
KSh0.00
₩0.00
DH0.00
L0.00
ден0.00
MOP$0.00
MX$0.00
RM0.00
N$0.00
₦0.00
C$0.00
NOK kr0.00
रु0.00
NZ$0.00
S/0.00
K0.00
₱0.00
₨0.00
zł0.00
₲0.00
L0.00
QR0.00
SAR0.00
SEK kr0.00
S$0.00
฿0.00
₺0.00
$U0.00
R0.00
ZK0.00
Already have an account? Log In

Transcript

Hi, everyone. So in the last video we saw the power of interfaces and how it helps us enforce some really good code style and grade guidelines. In this video I'm going to, I'm going to show you how what interfaces are used for besides enforcing style. So let's say you want to create a function, right? Let's say I want a function which says anything. Right?

So anything is a function which takes in anything and we're going to talk about the type later and then it doesn't do anything it just prints the type write it this is from dot print line. Anything. So one thing you need to know about empty empty interfaces is an empty interface is just like a black box. So an empty interface can be anything it can be a string, it can be a float, it can be anything you want, want it to be, so if you want if you don't know the type of the The function or the type of the argument of the function during runtime, or during compile time, rather than you can simply say that, okay, define it as an interface. And then I can print anything. So I can simply do any, I can do anything 2.44 I can do anything.

I can do anything, my name, which I never seem to get correctly. And I can also have it be a struct I see, so you're gonna run it real quick, or lean forward or go. And type struct is not an expression my bag because in with one records, we declared the struct and with another set of brackets, we declare that okay, this is an empty struct, we need to so this is a really neat thing. You're gonna see this a lot with a with interfaces also. So if you're talking about a struct, right, so how do you define In a struct, use a type struct, and then you define it right. So so what you might do is you need some, you might do a type something something, and then you might do struct.

And then you might have braces. So that is called a declaration. So instantiation or rather, taking an instance of the structure is done in this way. So you take a struct, and then this. So this is an empty structure. So let's say our structure is not empty or structure is person.

String, then you might instantiate this with a high so in our case, we have instantiated or empty slot. So we're just going to go run main folder go and voila, it ran. So you can really see the power of interfaces from this tutorial. That Okay, interfaces are also helpful in enforcing style. And interfaces help you when you don't know the type of the variable during compile time. It can be anything can be in float struct.

Anything. So this is really helpful when you're talking about web development, because in web development, you don't know what your input is going to be most of the time. So this is a really good thing. And then, so, you can also define map strings to interfaces. You can say that, okay, but he, or rather, my map is equal to make a map of a map string to an interface. We've seen the syntax before, right.

So mastering to interface means that the key or key of the map is always going to be a string with a value can be anything. So I can see my map. Name is equal to and then I can say my map age Go to 10 and then phone dot line. So let's see what happens no Wallah. So, age is an integer and name is a string it did anything without a hitch because we define that Okay, our key value can be string, but our key is going to be string but our value can be anything and interface which is a black box. So thank you and see you in the next video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.