To create a struct with a field of type string in Go, we can define the struct with the name of the struct and the name and type of the field. Here is an example:
main.go39 chars4 lines
We have created a struct called Person
with a field called Name
of type string
. We can now create instances of this struct and set the value of the Name
field like this:
main.go26 chars2 lines
We have created an instance of the Person
struct and set the value of the Name
field to "John"
. We can also access the value of the Name
field like this:
main.go36 chars2 lines
This will print the value of the Name
field, which is "John"
.
gistlibby LogSnag