Redmap is a general purpose Go module to convert structs to map of strings and vice versa, when possible.
One particular use case of Redmap is serializing and deserializing Redis object, which stores everything as strings.
- Keep the API similar to
encoding/json
: we do all hate learning yet another library - API stability
- Excellent test coverage
In a project using Go modules, run this in your terminal application:
go get github.com/livingsilver94/redmap
When the download finishes, you'll find Redmap under the unsurprising name of redmap
.
This example shows the simplest usage of Redmap. Usage of struct tags and unmarshaling pitfalls are better illustrated in the documentation.
package main
import (
"fmt"
"github.com/livingsilver94/redmap"
)
type MyStruct struct {
AString string
AnInt int
}
func main() {
// Struct to map.
myS := MyStruct{AString: "universe", AnInt: 42}
mp, err := redmap.Marshal(myS)
fmt.Println(mp, err)
// Map to struct.
var mySIsBack MyStruct
err = redmap.Unmarshal(mp, &mySIsBack)
fmt.Println(mySIsBack, err)
}
Although Redmap is a general-purpose library, it fits well the Redis use case, since this database stores everything as strings. Also, most IDE highlight strings in red 😀