Skip to content

Commit 3d07064

Browse files
committed
update: README.md
1 parent 7aca32a commit 3d07064

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

README.md

+78
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,80 @@
11
# structs
22
structure utils
3+
4+
# What is this?
5+
Look for struct fields that are nil.
6+
Used when the argument passed to the constructor etc. is a structure etc.
7+
8+
# Usage
9+
10+
### GetNilFields
11+
12+
```go
13+
package main
14+
15+
import (
16+
"log"
17+
18+
"github.com/go-utils/structs"
19+
)
20+
21+
type (
22+
Repository1 struct{}
23+
Repository2 struct{}
24+
Repository3 struct{}
25+
)
26+
27+
type ServiceRepositories struct {
28+
Repository1 *Repository1
29+
Repository2 *Repository2
30+
Repository3 *Repository3
31+
}
32+
33+
type Service struct {
34+
repos ServiceRepositories
35+
}
36+
37+
func NewService(repos ServiceRepositories) *Service {
38+
if nilFields := structs.GetNilFields(repos); len(nilFields) > 0 {
39+
log.Fatalf("%+v", nilFields)
40+
// Output: ["ServiceRepositories.Repository2", "ServiceRepositories.Repository3"]
41+
}
42+
return &Service{repos: repos}
43+
}
44+
45+
func main() {
46+
repos := ServiceRepositories{
47+
Repository1: new(Repository1),
48+
}
49+
service := NewService(repos)
50+
...
51+
}
52+
```
53+
54+
---
55+
56+
### GetStructName
57+
58+
```go
59+
package main
60+
61+
import (
62+
"fmt"
63+
"log"
64+
65+
"github.com/go-utils/structs"
66+
)
67+
68+
type Hoge struct{}
69+
70+
func main() {
71+
hoge := new(Hoge)
72+
hogeName := structs.GetStructName(hoge)
73+
fmt.Printf("struct name: %#v\n", hogeName)
74+
// Output: "Hoge"
75+
}
76+
```
77+
78+
## License
79+
- Under the [MIT License](./LICENSE)
80+
- Copyright (C) 2022 go-utils

0 commit comments

Comments
 (0)