An add-on for the Go 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 main
import (
"fmt"
"text/template"
"github.com/spf13/viper"
vipertemplate "github.com/sv-tools/viper-template/v2"
)
func main() {
v := viper.New()
v.Set("foo", `{{ Get "bar" }}`)
v.Set("bar", `{{ Mul . 2 }}`)
type Data struct {
Bar int
}
data := Data{
Bar: 42,
}
funcs := template.FuncMap{
"Mul": func(d *Data, v int) int {
return d.Bar * v
},
}
val, err := vipertemplate.Get(
v,
"foo",
vipertemplate.WithData(&data),
vipertemplate.WithFuncs(funcs),
)
if err != nil {
panic(err)
}
fmt.Println(val)
// Output: 84
}
% go test -bench=. -benchmem
goos: 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/op
goos: darwin
goarch: 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