Because it’s faster than JSON and has really great support in Golang thanks to msgp, Neoroute uses MessagePack as its encoding format of choice for everything.
This page gives you a short crash course into how to use it and known limitations with Neoroute.
Just run the following command to install the CLI for code generation (this is different from neogen, msgp is required to generate the code that make your structs encodable and decodable):
go install github.com/tinylib/msgp@latest
As with all of the times you use go install, make sure your $GOHOME/bin directory is in your path. Otherwise the following examples will not work.
Using msgp is actually not too different from json.
Just like JSON, you can declare any kind of struct, but instead of using the json struct tag, you now have to use the msg struct tag like this:
type Person struct {
Name string `msg:"name"`
Address string `msg:"address"`
Age int `msg:"age"`
Hidden string `msg:"-"` // this field is ignored
unexported bool // this field is also ignored
}
Now, to the file your struct is in you have to add the following line:
//go:generate msgp
This will tell go generate to run the specified command that is specified in the //go:generate comment. In this case, it will run msgp on the file.
You can now generate the encoding and decoding functions using:
go generate ./...
And just like this, you’re done!
Since msgp has lots of really cool features that you can all also use with Neoroute, except for the ones listed below, you can check out their wiki for everything that’s possible.
//msgp:tuple <struct> you can make the encoding and decoding of a lot of structs a lot faster. However, Neoroute does not currently support this feature very well, as our code generator for example still doesn’t know how to handle it.Since this will be asked anyway, here is a summary of all the reasons why we use MessagePack:
[]byte can be decoded without Base64 or some other kind of hack.We acknowledge the following downsides, but think that the positives far outweigh the negatives: