Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

An extension for Golang Viper library to use Go templates in viper config

License

NotificationsYou must be signed in to change notification settings

sv-tools/viper-template

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.

Code AnalysisGo ReferencecodecovGitHub tag (latest SemVer)

v2 Changes

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.

Usage

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}

Benchmarks

v2.0.0

using gov1.23.12

% 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/op

using gov1.25.0

goos: 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

Stars

Watchers

Forks

Contributors2

  •  
  •  

Languages


[8]ページ先頭

©2009-2025 Movatter.jp