define a struct in go

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.go
type Person struct {
    name string
    age  int
}
52 chars
5 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.go
var person struct {
    name string
    age  int
}
51 chars
5 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