Categorygithub.com/timothyl96/goo
repositorypackage
0.0.0-20231209180448-16149e14f6d0
Repository: https://github.com/timothyl96/goo.git
Documentation: pkg.go.dev

# Packages

No description provided by the author
No description provided by the author

# README

$$\textcolor{yellow}{\text{GOO}}$$

Golang standard library Extension + the OO way


The library you never knew you needed :speak_no_evil:


Codacy Badge Maintainability CodeFactor Go Report Card Go Reference

CircleCI Travis Build Status DeepSource

Codacy Badge codecov GitHub


There are 2 ways to use this library :bulb:

  1. Use the methods added to 'builtin' types so we can simplify calling functions in OO way.

    This includes builtin + extended functions like unique().

  2. :exclamation: Or, simply apply the extended functions in Go style.


Example - Extended functions in Go Style:

myInts := []int{1, 1, 1, 2, 3, 3}
goo.Unique(myInts) // returns {1, 2, 3}

// With String
myStrs := []string{"a", "a", "b", "b", "b", "b", "c"}
goo.Unique(myStrs) // returns {a, b, c}

// ForEach
goo.ForEach(myInts, func(i int) { fmt.Println(i) })

You can also use it in OOP style.

Example:

// ##### Example:
// ### Length:
str1 := goo.FromString("myString")
str1.length() // same as len(str1)

// ### Arithmetic and Itoa()
i1 := goo.FromInt(30)
i1 += 20
i1.Itoa() // same as strconv.Itoa(i1)

// ### ToUpper():
var str2 goo.String = "myuppercasestring"
str2.ToUpper() // same as strings.ToUpper(str2)

// ##### Slice
// ### Append()
s1 := goo.NewSlice(1, 2, 3)
s1 = s1.Append(4, 5, 6) // same as s1 = append(s1, 4, 5, 6)

// ##### Extension library example:
// ### Unique()
s2 := goo.NewSlice(1, 1, 2, 3, 3)
s2 = s2.Unique() // == [1, 2, 3]

// ### ForEach()
s3 := goo.NewSlice(1, 2, 3)
s3 = s3.ForEach(func(i int) { fmt.Println(i) }) // Prints each 1,2,3 in new line

// ### HasAnyPrefix()
s4 := goo.FromString("-abcdef")
_ = s4.HasAnyPrefix("x", "'", "=", "-") // == true


If typing goo. is too much, use dot import for this library:

import (
	. "github.com/timothyl96/goo"
)

:question::grey_question: How to declare a variable:

// Using Goo function
t1 := goo.FromString("myString")

// Using default = operator, but explicitly specify the type
var t2 goo.Int = 30

// Using explicit type conversion
t3 := goo.Int(2) 


// ### Declaring Slice
// Using NewSlice which returns a goo.Slice (recommended)
mySlice := goo.NewSlice(1, 2, 3)

// Using Goo function with Int
t5 := goo.FromSlice([]goo.Int{1, 2, 3})

// Using default = operation, but explicitly specify the type for the generic
var t6 goo.Slice[int] = []int{1, 2, 3}

// ### Declaring Map
// Capacity of 1, key int, value int - Recommended
t8 := goo.NewMap[int, int](1)
t8[1] = 1

// Convert from builtin map
t9 := goo.FromMap(make(map[int]int))

// or using var keyword
var t9 goo.Map[int, struct{}] = make(map[int]struct{})

More:

How to use Goo: GOO Example



Benchmark :clock330:

The result below shows that the performances are similar when compared to builtin functions.

Benchmarking result



Running the source code :runner:

There are 3 useful commands:

Run go generate for code generation :point_right: go generate ./...

  • This will generate most of the goo types e.g. string.go, int.go
  • Check the gen/ folder for more information

Run go test :point_right: go test -v ./...

Run go benchmark :point_right: go test -bench=Bench -run Bench -benchmem -benchtime 10s -v ./...



Contributing :memo:

Feel free to raise issue or PR