create a struct with a field of type string in go

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.go
type Person struct {
    Name string
}
39 chars
4 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.go
p := Person{Name: "John"}
26 chars
2 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.go
fmt.Println(p.Name) // Output: John
36 chars
2 lines

This will print the value of the Name field, which is "John".

related categories

gistlibby LogSnag