




This is a redux implementation in Go.
Origin version
$ go get github.com/dannypsnl/redux
import (// must import, else go module would think following package is a module _"github.com/dannypsnl/redux/v2"// v2 store"github.com/dannypsnl/redux/v2/store"// v2 rematch(optional)"github.com/dannypsnl/redux/v2/rematch")
Basic example
// As you can see, you don't need to checking nil or not now// Now redux-go will initial the state with zero value of that typefunccounter(stateint,actionstring)int {switchaction {case"INC":returnstate+1case"DEC":returnstate-1default:returnstate }}funcmain() {// redux/v2/storestore:=store.New(counter)store.Dispatch("INC")fmt.Printf("%d\n",store.StateOf(counter))// should print out: 1}
More stunning(lambda can work)
funcmain() {counter:=func(state,payloadint)int {returnstate+payload }store:=store.New(counter)store.Dispatch(100)store.Dispatch(-50)fmt.Printf("%d\n",store.StateOf(counter))// should print out: 50}
And more...
typeCountingModelstruct { rematch.ReducerStateint}func (cm*CountingModel)Increase(s,payloadint)int {returns+payload}func (cm*CountingModel)Decrease(s,payloadint)int {returns-payload}funcmain() {c:=&CountingModel{State:0, }store:=store.New(c)store.Dispatch(c.Action(c.Increase).With(100))store.Dispatch(c.Action(c.Decrease).With(50))fmt.Printf("result: %d\n",store.StateOf(c))// expect: 50}
Then let's have a party
typeCountingModelstruct { rematch.ReducerStateintIncrease*rematch.Action`action:"IncreaseImpl"`}func (c*CountingModel)IncreaseImpl(s,payloadint)int {returns+payload}funcmain() {c:=&CountingModel {State:0, }store:=store.New(c)store.Dispatch(c.Increase.With(30))store.Dispatch(c.Increase.With(20))fmt.Printf("result: %d\n",store.StateOf(c))// expect: 50}