Skip to content

proposal: time: new Init() and Start() methods for Timer #54870

@OmidHekayati

Description

@OmidHekayati

As suggested here #54861 by add these two new methods to time package, we achieve this goals:

  • Changes don't break any existing code and respect backward compatibility.
  • prevent two heap allocation (unnecessary) request, when a Timer can be part of other structs (embed or as a field).
  • Due to NewTimer can't inline and return the pointer of a Timer struct,
    If devs want to add a Timer to other structs (embed or as a field) to allocate both structs in one request,
    existing NewTimer() don't let us do this.
  • Init method can inline now and the Start() method sets the when field. In this way, functions respect the single responsibility principle.
/time> go build -gcflags="-m"
./sleep.go:64:6: can inline (*Timer).Init
./sleep.go:50:8: inlining call to (*Timer).Init
  • Improve code readability of NewTimer() function.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions