In Go, you can create a struct with multiple fields by defining a new type using the type
keyword, and specifying each field using its name and data type.
Here's an example of how to create a struct with two fields, name
and age
:
main.go51 chars5 lines
In this example, we've created a new type Person
, which has two fields: name
of type string
, and age
of type int
.
To create a new Person
object and initialize its fields, you can simply use the Person{}
syntax and provide values for each field:
main.go35 chars2 lines
This will create a new Person
object p
with the name
field equal to "John"
and the age
field equal to 30
.
You can access the fields of a struct using the dot (.
) operator:
main.go69 chars3 lines
I hope this helps you create your own structs with multiple fields in Go!
gistlibby LogSnag