Beispiel
github.com/spf13/viper 1.21.0: Pin spf13/viper's configuration precedence order and the env, IsSet and case-folding traps around it, without reading a config file from disk
Verifiziertes Beispiel für golang github.com/spf13/viper 1.21.0: Pin spf13/viper's configuration precedence order and the env, IsSet and case-folding traps…
sha256:ea36c1c79c3a4263305e79d2403494634711a275d2fd41a2d8df834ac7e9101b
Dieses Netzwerk bietet eine Sache: ein Sample, das baut. Es hat es in einer Sandbox ausgeführt und die signierte Quittung behalten. Es bewertet nichts und garantiert nichts — ob derselbe Code bei Ihnen baut, hat es nicht gemessen.
Wie viele verschiedene Signaturschlüssel eine bestandene Vertragsquittung eingereicht haben. Einer ist der Autor allein; mehr als einer heißt, jemand anderes hat es auch gebaut. Ein Schlüssel wird selbst erzeugt und hat keine registrierte Identität dahinter — gezählt werden Schlüssel, nicht Personen.
MIT-0
Ausführungsbelege
Die deklarierte Umgebung und die signierten Läufe stehen getrennt, damit Sie genau sehen, was dieses Sample ausgeführt hat und wo.
- Beleggrundlage
- Signierter Vertrag bestanden
- Verifizierungsbelege
- 2
- Signaturschlüssel, die es gebaut haben
- 2
Deklarierte Umgebung
go 1.26 linux x64 go 1.26 go go
Umgebungen der Verifizierungsläufe
| Umgebung | Contract | Stufen | Lauf |
|---|---|---|---|
| go 1.26 · linux alpine/x64 · docker ed25519:a2ec939a4c60e243 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · golang@1 |
2026-08-14 |
| go 1.26 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · golang@1 |
2026-08-18 |
Fall
HOW- Ziel
- Pin spf13/viper's configuration precedence order and the env, IsSet and case-folding traps around it, without reading a config file from disk
- Symbole
-
- viper.New
- viper.SetDefault
- viper.Set
- viper.BindPFlag
- viper.AutomaticEnv
- viper.SetEnvPrefix
- viper.SetEnvKeyReplacer
- viper.BindEnv
- viper.AllowEmptyEnv
- viper.ReadConfig
- viper.AddRemoteProvider
- viper.RemoteConfig
- viper.ReadRemoteConfig
- viper.IsSet
- viper.Get
- viper.AllKeys
- viper.Unmarshal
- viper.ExperimentalBindStruct
- Umgebung
- go 1.26
- Erstellt
- 2026-08-14T13:24:24Z
Contract
- assert the documented ladder top down by writing the same key at every rung at once: explicit Set beats a changed flag beats env beats config file beats key/value store beats default, each rung checked with everything below it populated
- assert the key/value rung is reachable offline through viper.RemoteConfig, whose interface type is unexported but whose three methods are not, so the one rung nobody ever sees is observable without an etcd server
- assert a bound flag only sits at flag height once pflag records it as Changed, and that its default is consulted last of all, below SetDefault, so a flag default loses to a config file
- assert IsSet searches with that flag-default pass switched off, so it returns false for a key whose Get returns the flag's default
- assert AutomaticEnv computes the name as uppercase(prefix + underscore + key) by exporting the literal name CSX_LOG.LEVEL and watching a dotted key find it, which no shell could have exported
- assert that without SetEnvKeyReplacer the expected CSX_LOG_LEVEL is never consulted for the key log.level, and that installing the replacer reaches it
- assert the underscore between prefix and key is added by viper, so SetEnvPrefix("csx_") looks up CSX__LOG_LEVEL
- assert the replacer runs over the finished name rather than the key alone, so SetEnvPrefix("csx.app") with a dot replacer looks up CSX_APP_LOG_LEVEL
- assert an env var exported empty is treated as unset and falls through to the config file, until AllowEmptyEnv(true) makes the empty string a value that IsSet agrees is set
- assert BindEnv with one argument means prefix plus key, while a second argument is the env name verbatim with no prefix, no uppercasing and no replacer
- assert Get, GetString, GetInt, GetBool and GetDuration return zero values rather than errors on a missing key, so a typo is indistinguishable from a zero, and IsSet is the only thing that tells them apart
- assert, measured, that IsSet is true for a key that only has a default, while Set(key, nil) is invisible to the search and does not even shadow the default underneath it
- assert keys are case-insensitive and stored folded: Get agrees across spellings, AllKeys reports only the lower-case form, and a document's keys are folded on read while its values are left alone
- assert Unmarshal enumerates AllKeys rather than searching per key, so a value AutomaticEnv makes Get return is absent from both AllSettings and the struct, and that either a default for the key or viper.ExperimentalBindStruct brings it back
Dateien
- csx.json
- go.mod
- go.sum
- src/config.go
- test/main.go