• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..06-Oct-2021-

.gitignoreH A D06-Oct-202141 54

.travis.ymlH A D06-Oct-2021141 1411

Gopkg.lockH A D06-Oct-2021748 2822

Gopkg.tomlH A D06-Oct-202175 43

LICENSEH A D06-Oct-20211.1 KiB2318

README.mdH A D06-Oct-20213.1 KiB7950

Taskfile.ymlH A D06-Oct-2021431 2722

accessors.goH A D06-Oct-20214.2 KiB172110

constants.goH A D06-Oct-2021325 145

conversions.goH A D06-Oct-20212.5 KiB10969

doc.goH A D06-Oct-20212.1 KiB671

map.goH A D06-Oct-20215.3 KiB194110

mutations.goH A D06-Oct-20212 KiB7549

security.goH A D06-Oct-2021315 1813

tests.goH A D06-Oct-2021362 1810

type_specific_codegen.goH A D06-Oct-202169.4 KiB2,5021,641

value.goH A D06-Oct-20211.4 KiB5746

README.md

1# Objx
2[![Build Status](https://travis-ci.org/stretchr/objx.svg?branch=master)](https://travis-ci.org/stretchr/objx)
3[![Go Report Card](https://goreportcard.com/badge/github.com/stretchr/objx)](https://goreportcard.com/report/github.com/stretchr/objx)
4[![Sourcegraph](https://sourcegraph.com/github.com/stretchr/objx/-/badge.svg)](https://sourcegraph.com/github.com/stretchr/objx)
5[![GoDoc](https://godoc.org/github.com/stretchr/objx?status.svg)](https://godoc.org/github.com/stretchr/objx)
6
7Objx - Go package for dealing with maps, slices, JSON and other data.
8
9Get started:
10
11- Install Objx with [one line of code](#installation), or [update it with another](#staying-up-to-date)
12- Check out the API Documentation http://godoc.org/github.com/stretchr/objx
13
14## Overview
15Objx provides the `objx.Map` type, which is a `map[string]interface{}` that exposes a powerful `Get` method (among others) that allows you to easily and quickly get access to data within the map, without having to worry too much about type assertions, missing data, default values etc.
16
17### Pattern
18Objx uses a preditable 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:
19
20    m, err := objx.FromJSON(json)
21
22NOTE: 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.
23
24Use `Get` to access the value you're interested in.  You can use dot and array
25notation too:
26
27     m.Get("places[0].latlng")
28
29Once you have sought the `Value` you're interested in, you can use the `Is*` methods to determine its type.
30
31     if m.Get("code").IsStr() { // Your code... }
32
33Or you can just assume the type, and use one of the strong type methods to extract the real value:
34
35    m.Get("code").Int()
36
37If 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.
38
39     Get("code").Int(-1)
40
41If 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.
42
43### Reading data
44A 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
57Since `objx.Map` is a `map[string]interface{}` you can treat it as such.  For example, to `range` the data, do what you would expect:
58
59    m := objx.MustFromJSON(json)
60    for key, value := range m {
61      // Your code...
62    }
63
64## Installation
65To install Objx, use go get:
66
67    go get github.com/stretchr/objx
68
69### Staying up to date
70To update Objx to the latest version, run:
71
72    go get -u github.com/stretchr/objx
73
74### Supported go versions
75We support the lastest two major Go versions, which are 1.8 and 1.9 at the moment.
76
77## Contributing
78Please feel free to submit issues, fork the repository and send pull requests!
79