To create a struct with a field of type array in Go, define a struct and the field that will contain an array of a specific type, along with the length of the array.
Here's an example of a struct that has a field of type array:
main.go64 chars5 lines
In the above example, the Person
struct has two fields - Name
of type string
and Address
of type array [2]string
. The Address
field has a length of 2, meaning it can store 2 string values.
To instantiate an object of this struct and set its values, you can use the following code:
main.go87 chars5 lines
In the above code, an instance of the Person
struct is created and its Name
and Address
fields are set to "John" and an array of strings containing "123 Main St" and "Apt 4C", respectively.
gistlibby LogSnag