- Marshaler">type Marshaler
- Unmarshaler">type Unmarshaler
- Decoder">type Decoder
- Encoder">type Encoder
msgpack与json类似,只不过,保存的2进制文件,相比json来说,数据更小
用法与json一致,通常与redis一起使用
文档:https://godoc.org/github.com/vmihailenco/msgpack
func Marshal(v interface{}) ([]byte, error)
func Unmarshal(data []byte, v interface{}) error
type Marshaler
type Marshaler interface {
MarshalMsgpack() ([]byte, error)
}
type Unmarshaler
type Unmarshaler interface {
UnmarshalMsgpack([]byte) error
}
type Decoder
从输入流解码对象
type Decoder struct {
// contains filtered or unexported fields
}
func NewDecoder(r io.Reader) Decoder
func (dec Decoder) Decode(v interface{}) error
type Encoder
将对象写入输出流
type Encoder struct {
// contains filtered or unexported fields
}
func NewEncoder(w io.Writer) Encoder
func (enc Encoder) Encode(v interface{}) error
import (
"fmt"
json "github.com/vmihailenco/msgpack"
)
type ConfigStruct struct {
Host string `json:"host"`
Port int `json:"port"`
}
func main() {
var con =ConfigStruct{"127.0.0.1",8080}
//struct 到json str
b, err := json.Marshal(con);
if err != nil{
return
}
fmt.Println(string(b))
//json str 转struct
var config ConfigStruct
if err := json.Unmarshal([]byte(b), &config); err == nil {
fmt.Println(config)//{http://localhost:9090 9090}
fmt.Println(config.Host)//http://localhost:9090
}
}
��Host�127.0.0.1�Port� �
{127.0.0.1 8080}
127.0.0.1