tailscale/tailscale

make go helper for "parsed string or fallback value" pattern

Open

#20.018 aperta il 5 giu 2026

Vedi su GitHub
 (1 commento) (1 reazione) (0 assegnatari)Go (2554 fork)batch import
good first issue

Metriche repository

Star
 (31.565 star)
Metriche merge PR
 (Merge medio 5g 22h) (173 PR mergiate in 30 g)

Descrizione

We have this pattern of "parse string S as type T, but return a fallback value V if parsing fails" all over the place for common T types like bool, time.Duration, etc. In case a util helper doesn't exist already, we should make one and make it discoverable.

func defDuration(a string, def time.Duration) time.Duration {
	if a == "" {
		return def
	}
	v, err := time.ParseDuration(a)
	if err != nil {
		return def
	}
	return v
}

Some examples in 5 mins of searching:

Guida contributor