We have discussed Golang struct in one of our previous posts, and this post will talk about the anonymous structs and fields in Golang.
Anonymous struct
Golang Anonymous struct is a normal struct without any name and thus cannot be accessed anywhere else in the program. Anonymous structs can be used to create only a single variable at a time and are very useful in situations where we don’t need the struct again in the program.
package main
import (
"fmt"
)
func main() {
person := struct {
firstName string
lastName string
age int
height float64
}{
firstName: "codekru",
lastName: "website",
age: 22,
height: 167.12,
}
fmt.Println("Person details:", person)
fmt.Println("Person's age:", person.age)
}
Output –
Person details: {codekru website 22 167.12}
Person's age: 22
Anonymous struct slices
We can also easily create the slices of an anonymous struct.
package main
import (
"fmt"
)
func main() {
persons := []struct {
firstName string
lastName string
age int
height float64
}{
{firstName: "codekru", lastName: "website", age: 22, height: 167.12},
{firstName: "golang", lastName: "tutorial", age: 25, height: 170.12},
{firstName: "anonymous", lastName: "struct", age: 26, height: 172.12},
}
fmt.Println("All Details:", persons)
fmt.Println("Details of first person:", persons[0])
fmt.Println("Age of first person:", persons[0].age)
}
Output –
All Details: [{codekru website 22 167.12} {golang tutorial 25 170.12} {anonymous struct 26 172.12}]
Details of first person: {codekru website 22 167.12}
Age of first person: 22
We can also declare an anonymous first and assign value to its fields afterward.
package main
import "fmt"
func main() {
var person struct {
firstName string
lastName string
age int
height float64
}
person.firstName = "codekru"
person.lastName = "website"
person.age = 23
person.height = 167.12
fmt.Println("Person Details:", person)
}
Output –
Person Details: {codekru website 23 167.12}
Anonymous fields
Sometimes, the code to access the inner or nested structs gets tedious as we have to write the field name every time we want to access the inner struct. Go provides us with the anonymous fields to save us from that.
Struct fields with no name and just the data type are anonymous fields. We can use an anonymous field to access the inner or nested struct easily.
type Company struct {
department string
Employee // Anonymous field
}
Code Example
package main
import (
"fmt"
)
type Employee struct {
firstName string
lastName string
age int
height float64
}
type Company struct {
department string
Employee // Anonymous field
}
func main() {
employee1 := Employee{"codekru", "website", 22, 167.12} // defining the employee struct
company1 := Company{"CS", employee1} // defining the company struct
// accessing the Employee anonymous field
// using the type itself
fmt.Println("Employee Details:", company1.Employee)
}
Output –
Employee Details: {codekru website 22 167.12}
We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.