Skip to content

Commit

Permalink
Add syntax highlighting in README
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeRoggenbuck authored Feb 8, 2025
1 parent 0c86e82 commit c824800
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,49 +19,62 @@ Objx provides the `objx.Map` type, which is a `map[string]interface{}` that expo
### Pattern
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:

m, err := objx.FromJSON(json)
```go
m, err := objx.FromJSON(json)
```

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.

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

m.Get("places[0].latlng")
```go
m.Get("places[0].latlng")
```

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

if m.Get("code").IsStr() { // Your code... }
```go
if m.Get("code").IsStr() { // Your code... }
```
Or you can just assume the type, and use one of the strong type methods to extract the real value:
m.Get("code").Int()
```go
m.Get("code").Int()
```
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.
Get("code").Int(-1)

```go
Get("code").Int(-1)
```
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.
### Reading data
A simple example of how to use Objx:
// Use MustFromJSON to make an objx.Map from some JSON
m := objx.MustFromJSON(`{"name": "Mat", "age": 30}`)
```go
// Use MustFromJSON to make an objx.Map from some JSON
m := objx.MustFromJSON(`{"name": "Mat", "age": 30}`)

// Get the details
name := m.Get("name").Str()
age := m.Get("age").Int()
// Get the details
name := m.Get("name").Str()
age := m.Get("age").Int()

// Get their nickname (or use their name if they don't have one)
nickname := m.Get("nickname").Str(name)
// Get their nickname (or use their name if they don't have one)
nickname := m.Get("nickname").Str(name)
```
### Ranging
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:
m := objx.MustFromJSON(json)
for key, value := range m {
// Your code...
}
```go
m := objx.MustFromJSON(json)
for key, value := range m {
// Your code...
}
```
## Installation
To install Objx, use go get:
Expand Down

0 comments on commit c824800

Please sign in to comment.