It is possible to create anonymous structs in Go. Anonymous structs do not have a name. They are created only once.
An anonymous struct is created only with the struct keyword. The declaration of the struct is followed by its initialization.
Go anonymous struct example
Here is a simple Golang anonymous struct example:
package main
import "fmt"
func main() {
u := struct {
firstName string
lastName string
email string
}{
firstName: "John",
lastName: "Cena",
email: "cena@gmail.com",
}
// The struct fields are accessed with the dot operator.
fmt.Println(u.firstName)
fmt.Println(u.lastName)
fmt.Println(u.email)
}
Output:
John Cena cena@gmail.com
Comments
Post a Comment