sft

command module
v0.0.0-...-341fef7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 4, 2021 License: MIT Imports: 14 Imported by: 0

README

sft

Go code generator for converting a time.Time value to a string representation.

Description

This program is designed to be run from the command line with a time formatting string, and it will emit Go source code that formats time.Time values according to the specified formatting string.

$ sft -m -o formatTime.go -f formatTime '%F %T'
$ go run formatTime.go
$ less formatTime.go

The resultant code can be copied and pasted into another Go source file, or the file can simply be compiled into another project. The name of the function it creates and the name of the package it uses can be changed on the command line. Removing the -m command line skips creating a main function, and outputs a single function with the specified function and package name.

The program could also be invoked from a Go generate statement in other Go source code.

//go:generate sft -f formatTime -o formatTime.go '%F %T'

Then when the time format spec changes, simply type go generate at the command line to regenerate the time formatting function.

Performance

It is a bit faster than the Go standard library time formatting functionality.

$ karrick@promethium sft % make clean bench
rm -f append copy sft append.go copy.go append_test.go copy_test.go
go build -o sft main.go
./sft -extra -f appendTime -append -o append_test.go RFC3339Nano
./sft -extra -f copyTime -o copy_test.go RFC3339Nano
go test -bench=. -benchmem main_test.go append_test.go copy_test.go
goos: darwin
goarch: amd64
cpu: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
BenchmarkAppendTime-16         	19457082	        55.87 ns/op	       0 B/op	       0 allocs/op
BenchmarkCopyTime-16           	21264758	        54.54 ns/op	       0 B/op	       0 allocs/op
BenchmarkStandardLibrary-16    	 5772063	       203.4 ns/op	      32 B/op	       1 allocs/op
PASS
ok  	command-line-arguments	3.764s

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
example module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL