In Go, you can define a struct using the type
keyword followed by the name of the struct and the keyword struct
. You can then define the fields of your struct within the curly braces.
Here's an example:
main.go52 chars5 lines
In the above example, we have defined a struct named Person
with two fields, name
and age
. The name
field is of type string
and the age
field is of type int
.
You can also define a struct without a name by using an anonymous struct. Here's an example:
main.go51 chars5 lines
In the above example, we have defined an anonymous struct with two fields, name
and age
. The name
field is of type string
and the age
field is of type int
.
gistlibby LogSnag