https://github.com/timothyl96/goo
Go standard library with extension - OOP style
https://github.com/timothyl96/goo
extension go go-method golang library linq object-oriented-programming oop
Last synced: 12 months ago
JSON representation
Go standard library with extension - OOP style
- Host: GitHub
- URL: https://github.com/timothyl96/goo
- Owner: TimothyL96
- License: apache-2.0
- Created: 2022-09-17T18:26:16.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2024-11-05T16:28:22.000Z (over 1 year ago)
- Last Synced: 2024-11-05T17:33:12.510Z (over 1 year ago)
- Topics: extension, go, go-method, golang, library, linq, object-oriented-programming, oop
- Language: Go
- Homepage:
- Size: 310 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# $$\textcolor{yellow}{\text{GOO}}$$
## **G**olang standard library Extension + the **OO** way
> The library you never knew you needed :speak_no_evil:
[](https://www.codacy.com/gh/TimothyL96/goo/dashboard?utm_source=github.com&utm_medium=referral&utm_content=TimothyL96/goo&utm_campaign=Badge_Grade)
[](https://codeclimate.com/github/TimothyL96/goo/maintainability)
[](https://www.codefactor.io/repository/github/timothyl96/goo)
[](https://goreportcard.com/report/github.com/timothyl96/goo)
[](https://pkg.go.dev/github.com/timothyl96/goo#section-documentation)
[](https://dl.circleci.com/status-badge/redirect/gh/TimothyL96/goo/tree/master)
[](https://app.travis-ci.com/TimothyL96/goo)
[](https://deepsource.io/gh/TimothyL96/goo/?ref=repository-badge)
[](https://www.codacy.com/gh/TimothyL96/goo/dashboard?utm_source=github.com&utm_medium=referral&utm_content=TimothyL96/goo&utm_campaign=Badge_Coverage)
[](https://codecov.io/gh/TimothyL96/goo)

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:
```Go
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:
```Go
// ##### 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:
```Go
import (
. "github.com/timothyl96/goo"
)
```
:question::grey_question: How to declare a variable:
```Go
// 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](https://github.com/TimothyL96/goo-example)
---
## Benchmark :clock330:
The result below shows that the performances are similar when compared to builtin functions.

---
## 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