1 /* 2 Package objx provides utilities for dealing with maps, slices, JSON and other data. 3 4 # Overview 5 6 Objx provides the `objx.Map` type, which is a `map[string]interface{}` that exposes 7 a powerful `Get` method (among others) that allows you to easily and quickly get 8 access to data within the map, without having to worry too much about type assertions, 9 missing data, default values etc. 10 11 # Pattern 12 13 Objx uses a predictable pattern to make access data from within `map[string]interface{}` easy. 14 Call one of the `objx.` functions to create your `objx.Map` to get going: 15 16 m, err := objx.FromJSON(json) 17 18 NOTE: Any methods or functions with the `Must` prefix will panic if something goes wrong, 19 the rest will be optimistic and try to figure things out without panicking. 20 21 Use `Get` to access the value you're interested in. You can use dot and array 22 notation too: 23 24 m.Get("places[0].latlng") 25 26 Once you have sought the `Value` you're interested in, you can use the `Is*` methods to determine its type. 27 28 if m.Get("code").IsStr() { // Your code... } 29 30 Or you can just assume the type, and use one of the strong type methods to extract the real value: 31 32 m.Get("code").Int() 33 34 If there's no value there (or if it's the wrong type) then a default value will be returned, 35 or you can be explicit about the default value. 36 37 Get("code").Int(-1) 38 39 If you're dealing with a slice of data as a value, Objx provides many useful methods for iterating, 40 manipulating and selecting that data. You can find out more by exploring the index below. 41 42 # Reading data 43 44 A simple example of how to use Objx: 45 46 // Use MustFromJSON to make an objx.Map from some JSON 47 m := objx.MustFromJSON(`{"name": "Mat", "age": 30}`) 48 49 // Get the details 50 name := m.Get("name").Str() 51 age := m.Get("age").Int() 52 53 // Get their nickname (or use their name if they don't have one) 54 nickname := m.Get("nickname").Str(name) 55 56 # Ranging 57 58 Since `objx.Map` is a `map[string]interface{}` you can treat it as such. 59 For example, to `range` the data, do what you would expect: 60 61 m := objx.MustFromJSON(json) 62 for key, value := range m { 63 // Your code... 64 } 65 */ 66 package objx 67