Skip to content

Commit 7b855fb

Browse files
committed
content: structs
1 parent 3449c16 commit 7b855fb

File tree

6 files changed

+146
-0
lines changed

6 files changed

+146
-0
lines changed

src/components/TopicList.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ const topics = {
5959
'early-returns',
6060
'wrapping-up',
6161
],
62+
structs: [
63+
'structs-in-go',
64+
'nested-structs',
65+
'anonymous-structs',
66+
'embedded-structs',
67+
'struct-methods',
68+
'wrapping-up',
69+
],
6270
};
6371

6472
export default TopicList;

src/data/structs/anonymous-structs.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Anonymous Structs
2+
3+
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:
4+
5+
```go
6+
myCar := struct {
7+
Make string
8+
Model string
9+
} {
10+
Make: "tesla",
11+
Model: "model 3"
12+
}
13+
```
14+
15+
You can even nest anonymous structs as fields within other structs:
16+
17+
```go
18+
type car struct {
19+
Make string
20+
Model string
21+
Height int
22+
Width int
23+
// Wheel is a field containing an anonymous struct
24+
Wheel struct {
25+
Radius int
26+
Material string
27+
}
28+
}
29+
```
30+
31+
## When Should you use an Anonymous struct?
32+
33+
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://www.willem.dev/articles/anonymous-structs/) if you're curious.

src/data/structs/embedded-structs.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Embedded Structs
2+
3+
Go is not an [object-oriented](https://en.wikipedia.org/wiki/Object-oriented_programming) language. However, embedded structs provide a kind of *data-only* inheritance that can be useful at times. Keep in mind, Go doesn't support classes or inheritance in the complete sense, embedded structs are just a way to elevate and share fields between struct definitions.
4+
5+
```go
6+
type car struct {
7+
make string
8+
model string
9+
}
10+
11+
type truck struct {
12+
// "car" is embedded, so the definition of a "truck" now also additionally contains all of the fields of the car struct
13+
car
14+
bedSize int
15+
}
16+
```
17+
18+
## Embedded vs Nested Struct
19+
20+
- An embedded struct's fields are accessed at the top level, unlike nested structs.
21+
- Promoted fields can be accessed like normal fields except that they can't be used in [composite literals](https://golang.org/ref/spec#Composite_literals)
22+
23+
```go
24+
lanesTruck := truck{
25+
bedSize: 10,
26+
car: car{
27+
make: "toyota",
28+
model: "camry",
29+
},
30+
}
31+
32+
fmt.Println(lanesTruck.bedSize)
33+
34+
// embedded fields promoted to the top-level
35+
// instead of lanesTruck.car.make
36+
fmt.Println(lanesTruck.make)
37+
fmt.Println(lanesTruck.model)
38+
```

src/data/structs/nested-structs.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Nested structs in Go
2+
3+
Structs can be nested to represent more complex entities:
4+
5+
```go
6+
type car struct {
7+
Make string
8+
Model string
9+
Height int
10+
Width int
11+
FrontWheel Wheel
12+
BackWheel Wheel
13+
}
14+
15+
type Wheel struct {
16+
Radius int
17+
Material string
18+
}
19+
```
20+
21+
The fields of a struct can be accessed using the dot `.` operator.
22+
23+
```go
24+
myCar := car{}
25+
myCar.FrontWheel.Radius = 5
26+
```

src/data/structs/struct-methods.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Struct Methods in Go
2+
3+
While Go is **not** object-oriented, it does support methods that can be defined on structs. Methods are just functions that have a receiver. A receiver is a special parameter that syntactically goes *before* the name of the function.
4+
5+
```go
6+
type rect struct {
7+
width int
8+
height int
9+
}
10+
11+
// area has a receiver of (r rect)
12+
func (r rect) area() int {
13+
return r.width * r.height
14+
}
15+
16+
r := rect{
17+
width: 5,
18+
height: 10,
19+
}
20+
21+
fmt.Println(r.area())
22+
// prints 50
23+
```
24+
25+
A receiver is just a special kind of function parameter. Receivers are important because they will allow us to define interfaces that our structs (and other types) can implement.

src/data/structs/structs-in-go.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Structs in Go
2+
3+
We use `structs` in Go to represent structured data. A struct type is defined with the keyword type, the name of the struct type, the keyword struct, and a pair of braces ({}). Within the braces, you list the fields in the struct. Just as you put the variable name first and the variable type second in a var declaration, you put the struct field name first and the struct field type second. It's often convenient to group different types of variables together. For example, if we want to represent a car we could do the following:
4+
5+
```go
6+
type car struct {
7+
Make string
8+
Model string
9+
Height int
10+
Width int
11+
}
12+
```
13+
14+
This creates a new struct type called `car`. All cars have a `Make`, `Model`, `Height` and `Width`. In Go, you will often use a `struct` to represent information that you would have used a dictionary for in Python, or an object literal for in JavaScript. You can define a struct type inside or outside of a function. A struct type that’s defined within a function can be used only within that function.
15+
16+
> If you already know an object-oriented language, you might be wondering about the difference between classes and structs. The difference is simple: Go doesn’t have classes, because it doesn’t have inheritance. This doesn’t mean that Go doesn’t have some of the features of object-oriented languages it just does things a little differently.

0 commit comments

Comments
 (0)