go
42 lines · 7 steps
Parsing timeout config in Go
A small config loader turns raw environment strings into validated durations with helpful errors and sane defaults.
Explained by
highlit
1package config
2
3import (
4 "fmt"
5 "time"
6)
7
8type Timeouts struct {
9 Read time.Duration
10 Write time.Duration
11 Shutdown time.Duration
12}
13
14func parseDuration(name, raw string, fallback time.Duration) (time.Duration, error) {
15 if raw == "" {
16 return fallback, nil
17 }
18 d, err := time.ParseDuration(raw)
19 if err != nil {
20 return 0, fmt.Errorf("config %q: invalid duration %q: %w", name, raw, err)
21 }
22 if d < 0 {
23 return 0, fmt.Errorf("config %q: duration must not be negative: %s", name, d)
24 }
25 return d, nil
26}
27
28func LoadTimeouts(env map[string]string) (Timeouts, error) {
29 var t Timeouts
30 var err error
31
32 if t.Read, err = parseDuration("READ_TIMEOUT", env["READ_TIMEOUT"], 15*time.Second); err != nil {
33 return Timeouts{}, err
34 }
35 if t.Write, err = parseDuration("WRITE_TIMEOUT", env["WRITE_TIMEOUT"], 15*time.Second); err != nil {
36 return Timeouts{}, err
37 }
38 if t.Shutdown, err = parseDuration("SHUTDOWN_TIMEOUT", env["SHUTDOWN_TIMEOUT"], 30*time.Second); err != nil {
39 return Timeouts{}, err
40 }
41 return t, nil
42}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Centralizing parsing in one helper keeps validation and error formatting consistent across every field.
- 2Wrapping errors with %w preserves the underlying cause while adding which config key failed.
- 3Returning a zero-value struct alongside the error forces callers to check err before using the result.
Related explainers
go
package streaming import ( "bufio"
Streaming NDJSON logs over HTTP in Go
http-streaming
channels
select
Advanced
10 steps
typescript
import { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import * as Joi from 'joi';
Validating env config at boot in NestJS
configuration
schema-validation
environment-variables
Intermediate
8 steps
php
<?php namespace App\Services\Checkout;
Validating coupons with Laravel's Pipeline
pipeline
chain of responsibility
transactions
Intermediate
7 steps
go
package api import ( "crypto/sha256"
ETag conditional requests in Gin
http-caching
etag
conditional-requests
Intermediate
6 steps
php
<?php namespace App\Services;
How a password strength validator works in PHP
validation
regular-expressions
data-driven
Intermediate
8 steps
go
func (w *Watcher) resetDebounce(d time.Duration) { if !w.timer.Stop() { select { case <-w.timer.C:
Debouncing a stream of events in Go
debounce
timers
channels
Advanced
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/parsing-timeout-config-in-go-explained-go-ddb8/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.