Add example usage as library in README (#560)

Add example usage as library in README
This commit is contained in:
Bruno Silva 2021-08-31 18:34:41 -03:00 committed by GitHub
parent b00b56c335
commit dde866ffbb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -34,6 +34,50 @@ brew install go-jsonnet
- id: jsonnet-lint
```
It can also be embedded in your own Go programs as a library:
```go
package main
import (
"fmt"
"log"
"github.com/google/go-jsonnet"
)
func main() {
vm := jsonnet.MakeVM()
snippet := `{
person1: {
name: "Alice",
welcome: "Hello " + self.name + "!",
},
person2: self.person1 { name: "Bob" },
}`
jsonStr, err := vm.EvaluateAnonymousSnippet("example1.jsonnet", snippet)
if err != nil {
log.Fatal(err)
}
fmt.Println(jsonStr)
/*
{
"person1": {
"name": "Alice",
"welcome": "Hello Alice!"
},
"person2": {
"name": "Bob",
"welcome": "Hello Bob!"
}
}
*/
}
```
## Build instructions (go 1.11+)
```bash