Skip to content

m-mizutani/gots

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gots: Go Tools Suite test gosec trivy Go Reference

Simple and easy, but tedious function set in Go.

Slice functions

Map

out := slice.Map([]string{"blue", "orange", "red"}, func(v string) int {
    return len(v)
})
fmt.Println(out)
// Output: [4 6 3]

Filter

out := slice.Filter([]int{1, 2, 3, 4, 5}, func(v int) bool {
    return v%2 == 0
})
fmt.Println(out)
// Output: [2 4]

Reduce

out := slice.Reduce([]int{1, 2, 3, 4, 5}, func(p, c int) int {
    return p + c
}, 10)
fmt.Println(out)
// Output: 25

Uniq

out := slice.Unique([]string{"A", "B", "A", "C", "B"})
fmt.Println(out)
// Output: [A B C]

Count

out := slice.Count([]string{"A", "B", "A", "C", "B"}, "B")
fmt.Println(out)
// Output: 2

CountIf

out := slice.CountIf([]string{"A", "B", "A", "C", "B"}, func(v string) bool {
    return v == "A" || v == "C"
})
fmt.Println(out)
// Output: 3

Contains

fmt.Println(slice.Contains([]string{"A", "B", "A", "C", "B"}, "B"))
// Output: true

fmt.Println(slice.Contains([]string{"A", "B", "A", "C", "B"}, "X"))
// Output: false

Flatten

	fmt.Println(slice.Flatten(
		[]string{"A", "B"},
		[]string{"C", "D", "E"},
		[]string{"F"},
	))
	// Output: [A B C D E F]

Random string

// Returns 6 random characters
fmt.Println(rands.New(6))
// Output: H9ZBvu

// Returns 32 random characters from "最高技術責任者"
fmt.Println(rands.New(32, "最高技術責任者"))
// Output: 技者責責任術任最高責責最者者最任術任術者最任最最高技術術責最高任

// Returns 6 random alphabet (both of lower and upper) characters
fmt.Println(rands.Alphabet(6))
// Output: UJieDx

// Returns 6 random lower case alphabet characters
fmt.Println(rands.LowerCase(6))
// Output: dgffld

// Returns 6 random upper case alphabet characters
fmt.Println(rands.UpperCase(6))
// Output: DUQPEK

// Returns 6 random number characters
fmt.Println(rands.Number(6))
// Output: 784323

Value to Pointer

type input struct {
    ts *int64
}

// Compile error
// i := input {
//     ts: &(time.Now().Unix()),
// }

i := input {
    ts: ptr.To(time.Now().Unix()),
}

Stack trace

Get caller

func hello() {
	fmt.Println(stack.GetCaller())
}

func ExampleGetCaller() {
	hello()
	// Output: github.com/m-mizutani/gots/test_test.ExampleGetCaller
}

Cache

	item1 := cache.NewItem[string]()
	item1.Set("")

	item2 := cache.NewItem[string]()

	fmt.Println(item1.HasCache(), "=>", item1.Get(), ";")
	fmt.Println(item2.HasCache(), "=>", item2.Get(), ";")
	// Output:
	// true =>  ;
	// false =>  ;

License

Apache License 2.0

About

Simple and easy, but tedious function set in Go.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages