Skip to content

Commit f3a7a99

Browse files
Add syntax highlighting in README (#153)
1 parent 0c86e82 commit f3a7a99

1 file changed

Lines changed: 30 additions & 17 deletions

File tree

README.md

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,49 +19,62 @@ Objx provides the `objx.Map` type, which is a `map[string]interface{}` that expo
1919
### Pattern
2020
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:
2121

22-
m, err := objx.FromJSON(json)
22+
```go
23+
m, err := objx.FromJSON(json)
24+
```
2325

2426
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.
2527

2628
Use `Get` to access the value you're interested in. You can use dot and array
2729
notation too:
2830

29-
m.Get("places[0].latlng")
31+
```go
32+
m.Get("places[0].latlng")
33+
```
3034

3135
Once you have sought the `Value` you're interested in, you can use the `Is*` methods to determine its type.
3236

33-
if m.Get("code").IsStr() { // Your code... }
37+
```go
38+
if m.Get("code").IsStr() { // Your code... }
39+
```
3440
3541
Or you can just assume the type, and use one of the strong type methods to extract the real value:
3642
37-
m.Get("code").Int()
43+
```go
44+
m.Get("code").Int()
45+
```
3846
3947
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.
4048
41-
Get("code").Int(-1)
42-
49+
```go
50+
Get("code").Int(-1)
51+
```
4352
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.
4453
4554
### Reading data
4655
A simple example of how to use Objx:
4756
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
59+
m := objx.MustFromJSON(`{"name": "Mat", "age": 30}`)
5060

51-
// Get the details
52-
name := m.Get("name").Str()
53-
age := m.Get("age").Int()
61+
// Get the details
62+
name := m.Get("name").Str()
63+
age := m.Get("age").Int()
5464

55-
// Get their nickname (or use their name if they don't have one)
56-
nickname := m.Get("nickname").Str(name)
65+
// Get their nickname (or use their name if they don't have one)
66+
nickname := m.Get("nickname").Str(name)
67+
```
5768
5869
### Ranging
5970
Since `objx.Map` is a `map[string]interface{}` you can treat it as such. For example, to `range` the data, do what you would expect:
6071
61-
m := objx.MustFromJSON(json)
62-
for key, value := range m {
63-
// Your code...
64-
}
72+
```go
73+
m := objx.MustFromJSON(json)
74+
for key, value := range m {
75+
// Your code...
76+
}
77+
```
6578
6679
## Installation
6780
To install Objx, use go get:

0 commit comments

Comments
 (0)