-1

I have the following Go code used to capture elapsed time:

import (
 "time"
)

start := time.Now()
...
end := time.Since(start)
log.Printf("Elapsed Time:  %s", end)

The output of which is:

2019/10/26 13:22:53 Elapsed Time :  41.867µs

I would like the output to be simply:

Elapsed Time :  41.867µs

Not sure how to supress the time packages default data/clock time output.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
HMLDude
  • 1,547
  • 7
  • 27
  • 47
  • 2
    An alternative to Cerise's answer is to use `fmt` instead of `log`. – mkopriva Oct 26 '19 at 18:37
  • Any idea of how I would force the output to always be expressed, purely in the form of millisecond instead of microseconds? Even if the wall time is several minutes in length, I would like the time to be expressed purely in milliseconds. – HMLDude Oct 27 '19 at 01:16
  • 1
    How a time.Duration is printed is implemented by the [`String`](https://golang.org/pkg/time/#Duration.String) method, which you cannot change. What you can do however is to use the `%d` verb and divide the duration by `time.Millisecond`. (https://play.golang.com/p/TFJhBeDA8hk) – mkopriva Oct 27 '19 at 08:27

1 Answers1

3

To log without a date and time, clear the date and time flags:

log.SetFlags(log.Flags() &^ (log.Ldate | log.Ltime))

Because the default flags are (log.Ldate | log.Ltime), the following also works:

log.SetFlags(0)
Charlie Tumahai
  • 113,709
  • 12
  • 249
  • 242