https://github.com/lab5e/goplate
Go template library
https://github.com/lab5e/goplate
Last synced: over 1 year ago
JSON representation
Go template library
- Host: GitHub
- URL: https://github.com/lab5e/goplate
- Owner: lab5e
- License: apache-2.0
- Created: 2021-09-23T10:14:05.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2023-03-12T14:55:29.000Z (over 3 years ago)
- Last Synced: 2024-06-20T07:55:45.849Z (about 2 years ago)
- Language: Go
- Size: 26.4 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# goplate - yet another go template library
Fields are enclosed in double curly braces like regular Go templates:
{{ fieldName }}
Nested data structures use dot separators.
{{ fieldName.subStructure.field }}
Template syntax is fairly obvious - the names match roughly the JSON
equivalent when marshalled. Names are case insensitive so `{{ fieldName }}` and
`{{ fieldname }}` resolves to the same field.
Map access is limited to `map[string]string` only and map access is also
fairly obvious if you are familiar with Go:
{{ mapName["name"] }}
## Transformation functions
There is a few transformations available in the templates. This marshals the
entire field as a JSON structure and includes it in the output
{{ fieldName | json }}
This formats an int64 nanosecond field to a string representation of the data:
{{ timestampField | asTime }}
This formats a byte buffer to a hex string:
{{ byteField | asHex }}
## Usage
This will build a template and execute it:
type testStruct struct {
Field1 int
Field2 int
}
// ...
tmpl, err := New(`{{ field1 }}/{{ field2 }}`).
WithParameters(&testStruct{}).
Build()
if err != nil {
panic()
}
buf := &bytes.Buffer{}
if err := tmpl.Execute(buf, &testStruct{Field1: 1, Field2: 2}) {
panic()
}
// Print buffer
fmt.Println(buf.String())