Documentation ¶
Overview ¶
Package stopwatch provides a timer that implements common stopwatch functionality.
Index ¶
- type Stopwatch
- func (s *Stopwatch) ElapsedTime() time.Duration
- func (s *Stopwatch) IsReseted() bool
- func (s *Stopwatch) IsStopped() bool
- func (s *Stopwatch) Lap() time.Duration
- func (s *Stopwatch) Laps() []time.Duration
- func (s *Stopwatch) Log(msg string)
- func (s *Stopwatch) MarshalJSON() ([]byte, error)
- func (s *Stopwatch) Print(msg string)
- func (s *Stopwatch) Reset()
- func (s *Stopwatch) Start(offset time.Duration)
- func (s *Stopwatch) Stop()
- func (s *Stopwatch) String() string
- func (s *Stopwatch) UnmarshalJSON(data []byte) (err error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Stopwatch ¶
type Stopwatch struct {
// contains filtered or unexported fields
}
Stopwatch implements the stopwatch functionality. It is not threadsafe by design and should be protected when there is a need for.
func New ¶
func New() *Stopwatch
New creates a new Stopwatch. To start the stopwatch Start() should be invoked.
func Start ¶
Start creates a new stopwatch with starting time offset by a user defined value. Negative offsets result in a countdown prior to the start of the stopwatch. A zero offset starts the stopwatch immediately.
func (*Stopwatch) ElapsedTime ¶
ElapsedTime returns the duration between the start and current time.
func (*Stopwatch) Lap ¶
Lap takes and stores the current lap time and returns the elapsed time since the latest lap.
func (*Stopwatch) Log ¶
Log calls log.Printf() with the given string and the elapsed time attached. Useful to use with a defer statement. Example : defer Start().Log("myFunction") Output: 2014/02/10 00:44:56 myFunction - elapsed: 2.000169591s
func (*Stopwatch) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface. The elapsed time is quoted as a string and is in the form "72h3m0.5s". For more info please refer to time.Duration.String().
func (*Stopwatch) Print ¶
Print calls fmt.Printf() with the given string and the elapsed time attached. Useful to use with a defer statement. Example : defer Start().Print("myFunction") Output : myFunction - elapsed: 2.000629842s
func (*Stopwatch) Reset ¶
func (s *Stopwatch) Reset()
Reset resets the timer. It needs to be started again with the Start() method.
func (*Stopwatch) Start ¶
Start resumes or starts the timer. If a Stop() was invoked it resumes the timer. If a Reset() was invoked it starts a new session with the given offset.
func (*Stopwatch) Stop ¶
func (s *Stopwatch) Stop()
Stop stops the timer. To resume the timer Start() needs to be called again.
func (*Stopwatch) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface. The elapsed time is expected to be a string that can be successful parsed with time.ParseDuration.