Structures in Go

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

Hello everyone, in this video, we are going to be talking about structures. So what is a structure? A structure structure is just an abstract data type, which groups together logically related data. So let's see that with an example. So we declare a struct by the syntax type, the name of the structure, and struct. We have created a new structure here.

So the way to initialize a variable with this structure, we can we can do either of the two things, you can do c is equal to car or you can do c one, c one. So you might have noticed that whenever we were declaring a variable, the syntax was variable, where the name of the variable and the data type. So what we can infer from this structure is essentially a data type, which groups together logically related data This is called Data encapsulation. Now we're going to be defining some member data types, member data variables inside of the car. So a car might have a name, which can be a string, it might have an H, which can be an integer. And also, it can also have a model number, which can also be an integer.

So, to define a car, we can initialize an empty structure, simply do this, and it's gonna initialize all of the all of the member data types as zero as a null type. So you can see a string is defined as this integers are defined as zero. So to initialize a structure as an object, you can simply follow this syntax to one good intro to that So, the name of the structure followed by curly braces and all the data types you can define in this way. One more technique is this, the Object Notation. So you can do a Chevy, you can do an age on it. And you can do more than number on it.

And just make sure that when you're writing like this, you always you always suffix the last data type the column Gouda and this. So, it is actually very desirable that you write it in this way, because in the case of very large structures, you won't be able to tell which data structure is which data type is which. So and one more thing is that you can also define methods on top of your structures. So the way to do that is you write a function, and instead of writing the function name, you write the name of the structure with an object And then you simply do simply write your function as you want to write and this function can print a guy right and then to call this function, you simply do a C dot print notice that these function this method is attached to the object of this structure.

So, this is a really nice way of logically relating all of the data which is related to one another. So a car has an age number and all a car also has a brand. A car can also have a function which can say okay, you have to drive. So I can also have a drive function driving a vehicle seat sudo sudo sudo tray Try not to return something from the function simply do a fun name of the structure and then get name. And what gate name does is it returns a string which you're going to see is going to be the name of the car. Now let's see what happens.

See your name? Yeah, you got the name of the car. So all of this data is related logically. And this is what is a structure. So see you in the next video, we are going to be talking about interfaces. Thank you.

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.