-
Notifications
You must be signed in to change notification settings - Fork 192
Working Demo
Klaus Post edited this page Oct 29, 2024
·
19 revisions
1.Make a temporary folder for this demo
$ mkdir temp
2.Install the msgp processor
$ go install github.com/tinylib/msgp@latest
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: (this is important as it generates a "template" for your msgp variables defined above)
$ go generate
5.Two more files are generated here, main_gen.go and main_gen_test.go , verify by typing
$ ls -al
6.Replace all content of main.go by copy pasting these lines:
package main
import (
"fmt"
)
//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.
fmt.Println(leftovervalues)
// leftovervalues is empty because bts only contained one object.
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 {
fmt.Printf("error: %s\n", err)
}
}
7.Run the following lines to generate "main" executable:
$ go build main.go main_gen.go
8.Run the program
$ ./main
9.You should see
[]
{1 9 table key value}
1
9
table
key
value
Nothing to see here yet!
10.That's it!