Skip to content

Commit 30640fe

Browse files
authored
Create README.md
1 parent d7d97e7 commit 30640fe

File tree

1 file changed

+241
-0
lines changed

1 file changed

+241
-0
lines changed

README.md

Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
# golang-default
2+
[![Build Status](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2FRebirthLee%2Fgolang-default%2Fbadge%3Fref%3Dmaster&style=flat)](https://actions-badge.atrox.dev/RebirthLee/golang-default/goto?ref=master)
3+
[![GitHub release (latest by date)](https://img.shields.io/github/v/release/rebirthlee/golang-default)](https://github.com/RebirthLee/golang-default/releases)
4+
[![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/rebirthlee/golang-default)](https://golang.org/doc/go1.13)
5+
[![license](https://img.shields.io/badge/license-BEER--WARE-green)](/LICENSE.md)
6+
7+
Initialize or New Struct with default value
8+
9+
## Setup
10+
```
11+
go get github.com/rebirthlee/golang-default
12+
```
13+
14+
## Support type
15+
- `int/8/16/32/64`, `uint/8/16/32/64`
16+
```go
17+
`def:"100"`
18+
`def:"-100"` // support only signed integer
19+
// support hexadecimal, octal, binary
20+
`def:"0xff"` // hexadecimal
21+
`def:"0xFF"` // hexadecimal
22+
`def:"0o77"` // octal
23+
`def:"0b11001111"` // binary
24+
```
25+
- `float32/64`
26+
```go
27+
`def:"3.141592653589"`
28+
`def:"-3.141592"`
29+
```
30+
- `complex64/128`
31+
```go
32+
// `def:"{real part},{imaginary part}"`
33+
`def:"3.14,-10"`
34+
`def:"-3.14,3"`
35+
```
36+
- Nested Struct
37+
```go
38+
type Parent struct {
39+
Name string `def:"Parent Struct"`
40+
OneChild Child `def:"dive"`
41+
TwoChild Child `def:"dive"`
42+
}
43+
44+
type Child struct {
45+
Key string `def:"unknown"`
46+
Number int `def:"-1"`
47+
}
48+
```
49+
50+
- Pointer of type
51+
```go
52+
type Parent struct {
53+
Name *string `def:"Parent Struct"`
54+
OneChild *Child `def:"dive"`
55+
TwoChild *Child `def:"dive"`
56+
}
57+
58+
type Child struct {
59+
Key *string `def:"unknown"`
60+
Number *int `def:"-1"`
61+
}
62+
```
63+
64+
- Array
65+
```go
66+
type Sample struct {
67+
Arr [3]int `def:"dive,-1"` // [-1, -1, -1]
68+
}
69+
```
70+
71+
- Slice
72+
```go
73+
type Sample struct {
74+
// `def:"dive({length},{capacity(optional)}),{value}"`
75+
Sli []int `def:"dive(5),-1"` // [-1, -1, -1, -1, -1]
76+
77+
//cap(SliWithCap) == 7
78+
SliWithCap []NestedSample `def:"dive(3,7),dive"` // [{nested struct},{nested struct},{nested struct}]
79+
}
80+
81+
type NestedSample struct {
82+
Name string `def:"nested struct"`
83+
}
84+
```
85+
86+
- Map
87+
```go
88+
type Sample struct {
89+
DiveMap map[string]*Struct `def:"dive{\"john doe\":dive,\"some one\":dive,\"key\":dive}"`
90+
/*
91+
{
92+
"john doe": &{who?},
93+
"some one": &{who?},
94+
"key": &{who?}
95+
}
96+
*/
97+
StructKeyMap map[*Struct]bool `def:"dive{dive:true,dive:false,dive:true}"`
98+
/*
99+
{
100+
&{who?}: true,
101+
&{who?}: false,
102+
&{who?}: true
103+
}
104+
*/
105+
DiveNestedMap map[string]map[*Struct]bool `def:"dive{\"key1\":dive{dive:true,dive:false},\"key2\":dive{dive:false,dive:false}}"`
106+
/*
107+
{
108+
"key1": {
109+
&{who?}: true,
110+
&{who?}: false
111+
},
112+
"key2": {
113+
&{who?}: false,
114+
&{who?}: false
115+
}
116+
}
117+
*/
118+
}
119+
120+
Struct struct {
121+
Name string `def:"who?"`
122+
}
123+
```
124+
125+
- Json
126+
```go
127+
type Sample struct {
128+
Arr [3]int `def:"[1,2,3]"` // [1,2,3]
129+
Sli []string `def:"[\"slice 1\",\"slice 2\"]"` // [slice 1,slice 2]
130+
Map map[string]interface{} `def:"{\"key1\":123,\"key2\":\"value\",\"nested map\":{\"key\":\"val\"}}"`
131+
/*
132+
{
133+
"key1":123,
134+
"key2":"value",
135+
"nested map":{
136+
"key":"val"
137+
}
138+
}
139+
*/
140+
141+
Nested NestedSample `def:"{\"displayName\":\"nested struct type\"}"` // {nested struct type}
142+
PtrNested *NestedSample `def:"{\"displayName\":\"nested struct pointer type\"}"` // &{nested struct pointer type}
143+
}
144+
145+
type NestedSample struct {
146+
Name string `json:"displayName"`
147+
}
148+
```
149+
150+
- Function
151+
152+
[Example](/example/func/main.go)
153+
154+
## Usage
155+
### Simple
156+
157+
```go
158+
import (
159+
"fmt"
160+
"github.com/rebirthlee/golang-default"
161+
)
162+
163+
type Person struct {
164+
Age int `def:"20"`
165+
Name string `def:"hellp"`
166+
}
167+
168+
...
169+
var p Person
170+
if err := def.Init(&p); err != nil {
171+
// error handle
172+
}
173+
fmt.Println(p) //out: {20 hellp}
174+
```
175+
176+
### Init
177+
If you got error, the next field of struct will not be initialized.
178+
179+
```go
180+
if err := def.Init(&p); err != nil {
181+
// error handle
182+
}
183+
```
184+
185+
### JustInit
186+
Even though it has an error, It will try to initialize all fields.
187+
And you can know that error field of struct.
188+
189+
```go
190+
if err := def.JustInit(&p); err != nil {
191+
justErr := err.(*def.ErrorJustInit)
192+
fmt.Println(justErr.Error())
193+
// error handle
194+
}
195+
```
196+
197+
### MustInit
198+
It isn't return error. but it will be panic when you has an error.
199+
200+
```go
201+
def.MustInit(&p) // hasn't return error.
202+
```
203+
204+
### New
205+
If you got error, it will be return nil.
206+
207+
```go
208+
i, err := def.New(Person{})
209+
if err != nil {
210+
// error handle
211+
} else {
212+
p := i.(*Person)
213+
fmt.Println(p) //out: &{20 hellp}
214+
}
215+
```
216+
217+
### JustNew
218+
Even though it has an error, It must return pointer of struct with error.
219+
220+
```go
221+
i, err := def.JustNew(Person{})
222+
if err != nil {
223+
justErr := err.(*def.ErrorJustInit)
224+
fmt.Println(justErr.Error())
225+
// error handle
226+
}
227+
p := i.(*Person)
228+
fmt.Println(p) //out: &{20 hellp}
229+
```
230+
231+
### MustNew
232+
It isn't return error. but it will be panic when you has an error.
233+
234+
```go
235+
p := def.MustNew(Person{}).(*Person) // hasn't return error.
236+
fmt.Println(p) //out: &{20 hellp}
237+
```
238+
239+
License
240+
---
241+
[`THE BEER-WARE LICENSE (Revision 42)`](http://en.wikipedia.org/wiki/Beerware)

0 commit comments

Comments
 (0)