Skip to content

Prefer time.Since Has Fix

Description

time.Since(start) states the intent directly and is equivalent to time.Now().Sub(start).

This syntactic rule assumes time is the unaliased standard-library package. It does not add or change imports. time.Since is available in Go 1.0 and newer.

YAML

yaml
id: use-time-since
language: Go
rule:
  pattern: time.Now().Sub($START)
fix: time.Since($START)

Example

go
import "time"

func elapsed(start time.Time) time.Duration {
  return time.Now().Sub(start)
}

Diff

go
import "time"

func elapsed(start time.Time) time.Duration {
  return time.Now().Sub(start) 
  return time.Since(start) 
}

Credits

Inspired by JetBrains' Go Modern Guidelines.

Made with ❤️ with Rust