State Management for Go Backend applications inspired in Redux.
╔═════════╗ ╔══════════╗ ╔═══════════╗ ╔═════════════════╗ ║ Action ║──────>║ Reducer ║ ────> ║ Store ║ ────> ║ Application ║ ╚═════════╝ ╚══════════╝ ╚═══════════╝ ╚═════════════════╝ ^ │ └────────────────────────────────────────────────────────────┘
go get github.com/luisvinicius167/godux
goduxturns your data flow unidirectional:
A Store is basically a container that holds your application state.
store := godux.NewStore() store.Setstate("count", 1) store.Setstate("Title", "I like godux!")
Actions are just pure function. Your Actions functions always return a godux.Action.
increment := func(number int) godux.Action { return godux.Action{ Type: "INCREMENT", Value: number, } }
Like Redux Concept: "Actions describe the fact that something happened, but don’t specify how the application’s state changes in response. This is the job of a reducer."
// reducer function reducer := func(action godux.Action) interface{} { switch action.Type { case "INCREMENT": return store.GetState("count").(int) + action.Value.(int) case "DECREMENT": return action.Value.(int) - store.GetState("count").(int) default: return store.GetAllState() } } // Add your reducer function to return new values basend on your state store.Reducer(reducer)
Dispatch the an action is very easy.
// Receive new value newCount := store.Dispatch(increment(1)) // return 2
godux.newStore()
`: Create a single store with the state of your application. godux.SetState(name string, value interface{})
`: Sets the state store. godux.GetState(name string)
`: Return your Store state value. godux.GetAllState()
`: Return all state value like a map type. store.Reducer(func(action godux.Action))
: Add the reducer function in your Store. store.Dispatch(action godux.Action)
: Dispatch your action to your Reducer. godux.Action( Type string, Value interface{})
: Your application action. MIT License.