Vasiliy Tolstov,

"I need to output strings with format YYYYMMDDHHMMSSMILLISEC without dot in 
string. But Go able to output milliseconds only in case of .000" ... "that 
works with many messages and want to avoid additional memory allocations"

The Go standard library does not pretend to provide everything you might 
need. Sometimes you need to do some programming. For example,

    func formatMillisecond(t time.Time) string

    fmtms.go
    https://play.golang.org/p/dfackUTL9qw

    $ go run fmtms.go

    20091110230000.001
    20091110230000001

A specialized function is often faster that a generalized function.

    fmtms_test.go
    https://play.golang.org/p/wS7PqZjExDq

    $ go test fmtms_test.go fmtms.go -bench=. -benchmem

    BenchmarkFmtMs-4   11470974   101 ns/op   32 B/op   1 allocs/op
    BenchmarkNoDot-4    4385608   272 ns/op   64 B/op   2 allocs/op
    BenchmarkDot-4      5529204   215 ns/op   32 B/op   1 allocs/op

Peter


On Saturday, July 18, 2020 at 6:49:23 PM UTC-4, Vasiliy Tolstov wrote:
>
> пт, 17 июл. 2020 г. в 05:40, Justin Israel <justin...@gmail.com 
> <javascript:>>: 
> > 
> > 
> > 
> > On Friday, July 17, 2020 at 11:21:32 AM UTC+12 va...@selfip.ru wrote: 
> >> 
> >> вт, 14 июл. 2020 г. в 18:39, Jake Montgomery <jake...@gmail.com>: 
> >> > 
> >> > I agree, it seems like an unfortunate oversight. You can do it 
> without the slice tricks: https://play.golang.org/p/tNAPOcQqApN 
> >> > It does take an additional Sprintf() though. 
> >> > 
> >> 
> >> Thanks, looks good. But i think for go devs adding additional format 
> >> that can be used to specify milli/micro/nano seconds will be easy. 
> >> 
> >> -- 
> >> Vasiliy Tolstov, 
> >> e-mail: v.to...@selfip.ru 
> > 
> > 
> > I asked the same question on reddit about a month ago, where I needed to 
> match an existing timestamp format: 
> > 
> https://www.reddit.com/r/golang/comments/gr7hz9/string_formatting_a_timetime_with_custom/
>  
> > 
> > Seems string formatting was the best option available to me. 
> > 
>
> Yes, this is bad case for stuff that works with many messages and 
> want to avoid additional memory allocations 
>
> -- 
> Vasiliy Tolstov, 
> e-mail: v.to...@selfip.ru <javascript:> 
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/e837d235-0227-49f5-97f8-738a6bf7d3ebo%40googlegroups.com.

Reply via email to