You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+30-17Lines changed: 30 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,49 +19,62 @@ Objx provides the `objx.Map` type, which is a `map[string]interface{}` that expo
19
19
### Pattern
20
20
Objx uses a predictable pattern to make access data from within `map[string]interface{}` easy. Call one of the `objx.` functions to create your `objx.Map` to get going:
21
21
22
-
m, err := objx.FromJSON(json)
22
+
```go
23
+
m, err:= objx.FromJSON(json)
24
+
```
23
25
24
26
NOTE: Any methods or functions with the `Must` prefix will panic if something goes wrong, the rest will be optimistic and try to figure things out without panicking.
25
27
26
28
Use `Get` to access the value you're interested in. You can use dot and array
27
29
notation too:
28
30
29
-
m.Get("places[0].latlng")
31
+
```go
32
+
m.Get("places[0].latlng")
33
+
```
30
34
31
35
Once you have sought the `Value` you're interested in, you can use the `Is*` methods to determine its type.
32
36
33
-
if m.Get("code").IsStr() { // Your code... }
37
+
```go
38
+
if m.Get("code").IsStr() { // Your code... }
39
+
```
34
40
35
41
Or you can just assume the type, and use one of the strong type methods to extract the real value:
36
42
37
-
m.Get("code").Int()
43
+
```go
44
+
m.Get("code").Int()
45
+
```
38
46
39
47
If there's no value there (or if it's the wrong type) then a default value will be returned, or you can be explicit about the default value.
40
48
41
-
Get("code").Int(-1)
42
-
49
+
```go
50
+
Get("code").Int(-1)
51
+
```
43
52
If you're dealing with a slice of data as a value, Objx provides many useful methods for iterating, manipulating and selecting that data. You can find out more by exploring the index below.
44
53
45
54
### Reading data
46
55
A simple example of how to use Objx:
47
56
48
-
// Use MustFromJSON to make an objx.Map from some JSON
49
-
m := objx.MustFromJSON(`{"name": "Mat", "age": 30}`)
57
+
```go
58
+
// Use MustFromJSON to make an objx.Map from some JSON
0 commit comments