Press "Enter" to skip to content

Golang Struct


so now we’re going to learn about strux
and a struct is a data structure that’s
where we get the name struct data
structure a struct is a data structure
which allows us to compose together
values of different types so it’s an
aggregate data type we could also say
it’s an aggregate data type it
aggregates together values of a
different type so let me just show you
how that works and then we’ll revisit
this definition it’ll make a little bit
more sense so we just go type and then
we give it a name we just create a name
and identifier and the underlying type
is struct and then we do curly braces
and we’re just going to put some fields
in here first is going to be a string
last is going to be a string and then I
think that’s good first and last and
then we can come down here we can create
values of that type this is a
user-defined type the type is person the
underlying type is struct so now we’re
gonna create values of that type and so
to use a composite literal we’re gonna
use the type and then curly braces and
so person 1 is going to be you know
their first name will be James and then
their last name will be bond and then we
could do person to well I guess let’s
type it out P to colon equals person the
type and then the curly brace is for a
composite that rolled their first name
is Miss and their last name is
Moneypenny and you have to have that
trailing comma there and that is the
preferred method for creating a value of
type person a value where the underlying
type is a struct you you initialize each
field on its own line so now I could
print those out value p1 value p2 and if
I run that I’m gonna see I’ve created
values of type person and they have
fields in there I could even access
those with dot notation so I could do p1
dot first p1 dot last and p2 dot first
and p2
and I can access those fields with dot
notation so that looks a lot like an
object to you or a class or we created
something and then we created instances
of it this is that’s that’s a good
approximation I don’t want to use those
words we don’t use those words and go we
don’t say object we don’t say class we
say we’ve created a value of type right
so this is a type person and now we’ve
created a value of type person so here
we created the value of type person and
we assigned it to the variable p1 so you
create values of a certain type so
that’s the first introduction to
creating a struct and that’s how structs
are used and they could a lot they allow
you to aggregate together values of
different types so we put in different
values in here write different fields
different types we could do age if we
want it so I could come up here and do
age hint and I could come down here and
I could do age and add in I don’t know
32 and I could do age here and add in 27
and then I could print out their age as
if I wanted so it’s putting together a
grenading together composing together
composing together values of different
types that’s what a struct allows us to
do create a data structure with values
of a different type so these are these
are this is known as a composite data
structure we’re composing together
values of a different type it can also
be referred to as an aggregate data
structure or a complex data structure
and so I have all of that right here a
struct is a composite data type
composite data types AKA aggregate data
types
also known as is AKA and AKA complex
data types structs allow us to compose
together values of a different type so
that’s your first step to understanding
Struck’s
[Music]
Please follow and like us:

Be First to Comment

Leave a Reply