fmtx

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2025 License: MIT Imports: 8 Imported by: 0

README

fmtx

More intuitive printing values for Golang.

Usage

go get github.com/mengdu/fmtx
type Foo struct {
  Key  int
  Loop *Foo
}

func main() {
  fmtx.Println([]int{1, 2, 3, 4}) // [4/4]int[1, 2, 3, 4]
  fmtx.Println(map[any]any{"a": 1, "b": 3.14, 1: true, "c": "Hello, \nworld."}) // map<any,any>{"a": 1, "b": 3.14, 1: true, "c": "Hello, \nworld."}
  foo := Foo{Key: 123}
  foo.Loop = &foo
  fmtx.Println(foo) // main.Foo{Key: 123, Loop: &main.Foo{Key: 123, Loop: &main.Foo{Key: 123, Loop: &main.Foo{…}}}}
}

Examples

Benchmark

go test -bench=. -run=^$ -benchmem -benchtime=5s
goos: darwin
goarch: amd64
pkg: github.com/mengdu/fmtx
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkFmtxString-12                           1951065              3022 ns/op            1112 B/op         58 allocs/op
BenchmarkFmtxStringDisableColor-12               3332950              1812 ns/op             360 B/op         18 allocs/op
BenchmarkSprintf1-12                             3172249              1800 ns/op             272 B/op         10 allocs/op
BenchmarkSprintf2-12                             3256015              1987 ns/op             272 B/op         10 allocs/op
BenchmarkSprintf3-12                             2901669              2073 ns/op             336 B/op         10 allocs/op
BenchmarkFmtxStringBig-12                          97717             66711 ns/op           24240 B/op       1308 allocs/op
BenchmarkFmtxStringDisableColorBig-12             179902             30867 ns/op            7506 B/op        326 allocs/op
BenchmarkSprintfBig1-12                            31928            212421 ns/op           83876 B/op       2149 allocs/op
BenchmarkSprintfBig2-12                            27352            197811 ns/op           85117 B/op       2205 allocs/op
BenchmarkSprintfBig3-12                            27866            206434 ns/op           89727 B/op       2205 allocs/op
PASS
ok      github.com/mengdu/fmtx  77.373s

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultWriter = os.Stdout

Functions

func Color added in v0.2.0

func Color(s string, start string, end string) string

func New added in v0.2.0

func New(opt *Options) func(v any) string

create a custom options function

func Println

func Println(a ...any) (n int, err error)

func ResetColorCache added in v0.2.0

func ResetColorCache()

reset enable color cache

func SetEnableColor added in v0.2.0

func SetEnableColor(enable bool)

func SetForceColor added in v0.2.0

func SetForceColor(force bool)

func String

func String(o any) string

Types

type ColorMap added in v0.2.0

type ColorMap struct {
	Int      [2]string
	Float    [2]string
	Complex  [2]string
	Bool     [2]string
	String   [2]string
	Ptr      [2]string
	Property [2]string
	FuncTag  [2]string
	Func     [2]string
	Chan     [2]string
	Nil      [2]string
	Tip      [2]string
}

type Options

type Options struct {
	// print max depth of struct, map, slice, array
	MaxDepth uint
	// print max array length
	MaxArray int
	// max property triggering line breaks
	MaxPropertyBreakLine int
	// show struct method
	ShowStructMethod bool
	// show type name
	ShowTypeName bool
	// default color map
	ColorMap ColorMap
	// coustom color function
	Color func(s string, start string, end string) string
	// custom prints
	Prints []func(v any, opt Options) (string, bool)
}
var Default Options = Options{
	MaxDepth:             3,
	MaxArray:             25,
	MaxPropertyBreakLine: 10,
	ColorMap: ColorMap{
		Int:      [2]string{"34", "39"},
		Float:    [2]string{"36", "39"},
		Complex:  [2]string{"35", "39"},
		String:   [2]string{"32", "39"},
		Bool:     [2]string{"33", "39"},
		Ptr:      [2]string{"33", "39"},
		Property: [2]string{"39", "39"},
		FuncTag:  [2]string{"3;33", "39;23"},
		Func:     [2]string{"3;36", "39;23"},
		Chan:     [2]string{"31", "39"},
		Nil:      [2]string{"93", "39"},
		Tip:      [2]string{"2", "22"},
	},
	Color: Color,
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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