Skip to content
unisqu edited this page Jun 13, 2018 · 19 revisions

1.Make a temporary folder for this demo

 $ mkdir temp

2.Download library

 $ go get -u -t github.com/tinylib/msgp

3.Open editor and copy/paste and save the code below:

 package main

 import (
     "fmt"
 )

 //go:generate msgp

 type QryD struct {
         A int8 
         D int8 
         T string 
         K string 
         V string 
 }

 func main() {
     fmt.Println("Nothing to see here yet!")
 }

4.Run the following command:

 $ go generate

5.Two more files are generated here, main_ge.go and main_gen_test.go , verify by typing

 $ ls -al
  1. Edit main.go by copy pasting these lines:

    package main

    import ( "fmt" "github.com/tinylib/msgp/msgp"

    )

    //go:generate msgp

    type QryD struct { A int8 D int8 T string K string V string }

    func main() {

     var valuePlaceholder QryD
    
     v := QryD{A: 1, D: 9, T: "table", K: "key", V: "value"}
     bts, err := v.MarshalMsg(nil)
     if err != nil {
    

    // fmt.Println(err) // print err or log error information } leftovervalues, err := valuePlaceholder.UnmarshalMsg(bts) //purpose of lefovervalues, pls read doc. //leftovervalues does not hold QryD, it holds the number of slices of QryD

     fmt.Println(valuePlaceholder)
     fmt.Println(valuePlaceholder.A)
     fmt.Println(valuePlaceholder.D)
     fmt.Println(valuePlaceholder.T)
     fmt.Println(valuePlaceholder.K)
     fmt.Println(valuePlaceholder.V)
    
     if err != nil {
    

    // t.Fatal(err) } if len(leftovervalues) > 0 { }else{ }

     leftovervalues, err = msgp.Skip(bts)
     if err != nil {
    

    // t.Fatal(err) } if len(leftovervalues) > 0 { // t.Errorf("%d bytes leftovervalues left over after Skip(): %q", len(leftovervalues), leftovervalues) }

     fmt.Println("Nothing to see here yet!")
    

    }

Clone this wiki locally