From c4315cd0e1e4085516219a2a7e8e5e6ccafcf821 Mon Sep 17 00:00:00 2001 From: Dan Kegel Date: Fri, 23 Apr 2021 18:58:05 -0700 Subject: [PATCH] README.md: add fully-worked tiny example, mention that it works in tinygo --- README.md | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/README.md b/README.md index 914b7d8..cc9f10f 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,50 @@ go generate ./... ``` This is most of what you need to know about go generate, but you can sese more about [go generate on the golang blog](http://blog.golang.org/generate). +## Complete minimal example + +Here's a working minimal example. Create demo/demo.go containing the struct to be marshalled, in its own module: +```Go +package demo + +//go:generate ffjson -nodecoder demo.go + +type AwesomeStruct struct { + Foo string + Bar string +} +``` +Then create main.go to marshal and print one of those structs: +```Go +package main + +import ( + "fmt" + "log" + "example.com/jsondemo/demo" +) + +func main() { + foo := demo.AwesomeStruct{ + Foo: "buz", + Bar: "", + } + marshaled, err := foo.MarshalJSON() + if err != nil { + log.Fatal(err) + } + fmt.Printf("%s\n", marshaled) // => {"foo":"buz"} +} +``` +Finally, create a go.mod, add ffjson to it, generate the marshaling code, and run: +```sh +$ go mod init example.com/jsondemo +$ go get -u github.com/pquerna/ffjson +$ (cd demo; go generate) +$ go run main.go +``` +This example also works with [tinygo](https://github.com/tinygo-org/tinygo/)! + ## Should I include ffjson files in VCS? That question is really up to you. If you don't, you will have a more complex build process. If you do, you have to keep the generated files updated if you change the content of your structs.