mirror of
https://github.com/bootdotdev/fcc-learn-golang-assets.git
synced 2025-12-18 19:21:17 +00:00
first
This commit is contained in:
39
course/4-structs/exercises/3-anonymous_structs/readme.md
Normal file
39
course/4-structs/exercises/3-anonymous_structs/readme.md
Normal file
@@ -0,0 +1,39 @@
|
||||
# Anonymous Structs in Go
|
||||
|
||||
An anonymous struct is just like a normal struct, but it is defined without a name and therefore cannot be referenced elsewhere in the code.
|
||||
|
||||
To create an anonymous struct, just instantiate the instance immediately using a second pair of brackets after declaring the type:
|
||||
|
||||
```go
|
||||
myCar := struct {
|
||||
Make string
|
||||
Model string
|
||||
} {
|
||||
Make: "tesla",
|
||||
Model: "model 3"
|
||||
}
|
||||
```
|
||||
|
||||
You can even nest anonymous structs as fields within other structs:
|
||||
|
||||
```go
|
||||
type car struct {
|
||||
Make string
|
||||
Model string
|
||||
Height int
|
||||
Width int
|
||||
// Wheel is a field containing an anonymous struct
|
||||
Wheel struct {
|
||||
Radius int
|
||||
Material string
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## When should you use an anonymous struct?
|
||||
|
||||
In general, *prefer named structs*. Named structs make it easier to read and understand your code, and they have the nice side-effect of being reusable. I sometimes use anonymous structs when I *know* I won't ever need to use a struct again. For example, sometimes I'll use one to create the shape of some JSON data in HTTP handlers.
|
||||
|
||||
If a struct is only meant to be used once, then it makes sense to declare it in such a way that developers down the road won’t be tempted to accidentally use it again.
|
||||
|
||||
You can read more about [anonymous structs here](https://blog.boot.dev/golang/anonymous-structs-golang/) if you're curious.
|
||||
Reference in New Issue
Block a user