Giter VIP home page Giter VIP logo

gots's Introduction

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

gots's People

Contributors

m-mizutani avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.