- Notifications
You must be signed in to change notification settings - Fork0
An extension for Golang Viper library to use Go templates in viper config
License
sv-tools/viper-template
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
An add-on for theGo Viper library that evaluates Go text/template expressions inside configuration values. Use it to reference other keys, inject custom data and functions, and compute settings dynamically at read time.
In v2, Get and GetString accept a Viper instance as the first parameter. The Viper interface abstracts the dependency, allowing this package to use one Viper version (e.g., for testing) while callers use another.
The Viper version pinned in go.mod is for internal testing only and does not constrain the Viper version you use in your project.
go get github.com/sv-tools/viper-template/v2@latest
package mainimport ("fmt""text/template""github.com/spf13/viper" vipertemplate"github.com/sv-tools/viper-template/v2")funcmain() {v:=viper.New()v.Set("foo",`{{ Get "bar" }}`)v.Set("bar",`{{ Mul . 2 }}`)typeDatastruct {Barint }data:=Data{Bar:42, }funcs:= template.FuncMap{"Mul":func(d*Data,vint)int {returnd.Bar*v }, }val,err:=vipertemplate.Get(v,"foo",vipertemplate.WithData(&data),vipertemplate.WithFuncs(funcs), )iferr!=nil {panic(err) }fmt.Println(val)// Output: 84}
% gotest -bench=. -benchmemgoos: darwin goarch: arm64 pkg: github.com/sv-tools/viper-template/v2 cpu: Apple M1 BenchmarkGetParallel-8 742156 1632 ns/op 4330 B/op 43 allocs/op BenchmarkGetSequential-8 406408 2802 ns/op 4328 B/op 43 allocs/opgoos: darwingoarch: arm64 pkg: github.com/sv-tools/viper-template/v2 cpu: Apple M1 BenchmarkGetParallel-8 749420 1542 ns/op 4457 B/op 45 allocs/op BenchmarkGetSequential-8 457682 2501 ns/op 4454 B/op 45 allocs/op
About
An extension for Golang Viper library to use Go templates in viper config
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.