create a struct in go

To create a struct in Go, you use the type keyword followed by the name of the struct and its definition using curly braces. Here's an example:

main.go
type Person struct {
    FirstName string
    LastName  string
    Age       int
}
83 chars
6 lines

In this example, we've created a struct named Person with three properties: FirstName, LastName, and Age. Each property has its own data type.

To use this struct, you can create a new instance of it like this:

main.go
p := Person{FirstName: "John", LastName: "Doe", Age: 30}
57 chars
2 lines

You can then access the properties of the struct using dot notation like this:

main.go
fmt.Println(p.FirstName) // output: John
fmt.Println(p.LastName)  // output: Doe
fmt.Println(p.Age)       // output: 30
120 chars
4 lines

Structs are a powerful way to group related data together in Go.

gistlibby LogSnag