goo

package module
v0.0.0-...-16149e1 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2023 License: Apache-2.0 Imports: 6 Imported by: 1

README ΒΆ

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

Golang standard library Extension + the OO way


The library you never knew you needed πŸ™Š


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 πŸ’‘

  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. ❗ 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"
)

❓❔ 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 πŸ•ž

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

Benchmarking result



Running the source code πŸƒ

There are 3 useful commands:

Run go generate for code generation πŸ‘‰ 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 πŸ‘‰ go test -v ./...

Run go benchmark πŸ‘‰ go test -bench=Bench -run Bench -benchmem -benchtime 10s -v ./...



Contributing πŸ“

Feel free to raise issue or PR

Documentation ΒΆ

Overview ΒΆ

Code generated by go generate; DO NOT EDIT. This file was generated at 2022-09-19 03:03:33.8231474 +0800 +08 m=+0.007318901

Code generated by go generate; DO NOT EDIT. This file was generated at 2022-09-19 03:03:35.9667562 +0800 +08 m=+0.005452501

Code generated by go generate; DO NOT EDIT. This file was generated at 2022-09-19 03:03:30.712158 +0800 +08 m=+0.005998601

Code generated by go generate; DO NOT EDIT. This file was generated at 2022-09-19 03:03:29.0859221 +0800 +08 m=+0.005734501

Code generated by go generate; DO NOT EDIT.

Code generated by go generate; DO NOT EDIT. This file was generated at 2022-09-19 03:03:32.1658398 +0800 +08 m=+0.006479901

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

func ForEach ΒΆ

func ForEach[T any](s Slice[T], exec func(T))

Types ΒΆ

type Bool ΒΆ

type Bool bool

func FromBool ΒΆ

func FromBool(n bool) Bool

FromBool converts builtin type to Goo's bool

func HasAnyPrefix ΒΆ

func HasAnyPrefix(s String, prefixes ...string) Bool

HasAnyPrefix returns true if the given string matched any of the prefixes

func (Bool) ToBool ΒΆ

func (n Bool) ToBool() bool

ToBool converts instance to builtin bool

type Byte ΒΆ

type Byte byte

func FromByte ΒΆ

func FromByte(n byte) Byte

FromByte converts builtin byte to Byte

func (Byte) ToByte ΒΆ

func (n Byte) ToByte() byte

ToByte converts instance to builtin uint8

type Complex128 ΒΆ

type Complex128 complex128

func FromComplex128 ΒΆ

func FromComplex128(n complex128) Complex128

FromComplex128 converts builtin type to Goo's complex128

func (Complex128) ToComplex128 ΒΆ

func (n Complex128) ToComplex128() complex128

ToComplex128 converts instance to builtin complex128

type Complex64 ΒΆ

type Complex64 complex64

func FromComplex64 ΒΆ

func FromComplex64(n complex64) Complex64

FromComplex64 converts builtin type to Goo's complex64

func (Complex64) ToComplex64 ΒΆ

func (n Complex64) ToComplex64() complex64

ToComplex64 converts instance to builtin complex64

type Float32 ΒΆ

type Float32 float32

func FromFloat32 ΒΆ

func FromFloat32(n float32) Float32

FromFloat32 converts builtin type to Goo's float32

func (Float32) ToFloat32 ΒΆ

func (n Float32) ToFloat32() float32

ToFloat32 converts instance to builtin float32

type Float64 ΒΆ

type Float64 float64

func FromFloat64 ΒΆ

func FromFloat64(n float64) Float64

FromFloat64 converts builtin type to Goo's float64

func (Float64) ToFloat64 ΒΆ

func (n Float64) ToFloat64() float64

ToFloat64 converts instance to builtin float64

type Int ΒΆ

type Int int

func FromInt ΒΆ

func FromInt(n int) Int

FromInt converts builtin type to Goo's int

func (Int) Itoa ΒΆ

func (i Int) Itoa() String

Itoa converts current Integer to ASCII string

func (Int) ToInt ΒΆ

func (n Int) ToInt() int

ToInt converts instance to builtin int

type Int16 ΒΆ

type Int16 int16

func FromInt16 ΒΆ

func FromInt16(n int16) Int16

FromInt16 converts builtin type to Goo's int16

func (Int16) ToInt16 ΒΆ

func (n Int16) ToInt16() int16

ToInt16 converts instance to builtin int16

type Int32 ΒΆ

type Int32 int32

func FromInt32 ΒΆ

func FromInt32(n int32) Int32

FromInt32 converts builtin type to Goo's int32

func (Int32) ToInt32 ΒΆ

func (n Int32) ToInt32() int32

ToInt32 converts instance to builtin int32

type Int64 ΒΆ

type Int64 int64

func FromInt64 ΒΆ

func FromInt64(n int64) Int64

FromInt64 converts builtin type to Goo's int64

func (Int64) ToInt64 ΒΆ

func (n Int64) ToInt64() int64

ToInt64 converts instance to builtin int64

type Int8 ΒΆ

type Int8 int8

func FromInt8 ΒΆ

func FromInt8(n int8) Int8

FromInt8 converts builtin type to Goo's int8

func (Int8) ToInt8 ΒΆ

func (n Int8) ToInt8() int8

ToInt8 converts instance to builtin int8

type Map ΒΆ

type Map[T constraints.Ordered, V any] map[T]V

func FromMap ΒΆ

func FromMap[T constraints.Ordered, V any](m map[T]V) Map[T, V]

FromMap converts builtin map of any time to goo.Map

func NewMap ΒΆ

func NewMap[T constraints.Ordered, V any](size ...Int) Map[T, V]

NewMap creates a new map with optional capacity

func (Map[T, V]) ToMap ΒΆ

func (m Map[T, V]) ToMap() map[T]V

ToMap converts goo.Map object to builtin map

type Rune ΒΆ

type Rune rune

func FromRune ΒΆ

func FromRune(n rune) Rune

FromRune converts builtin rune to Rune

func (Rune) ToRune ΒΆ

func (n Rune) ToRune() rune

ToInt32 converts instance to builtin int32

type Slice ΒΆ

type Slice[T any] []T

func FromSlice ΒΆ

func FromSlice[T any](slice []T) Slice[T]

FromSlice converts builtin slice of any time to goo.Slice object

func NewSlice ΒΆ

func NewSlice[T any](elements ...T) Slice[T]

NewSlice creates a new slice

func Unique ΒΆ

func Unique[T constraints.Ordered](s Slice[T]) Slice[T]

Unique function that is not tied to a slice

func (Slice[T]) Append ΒΆ

func (s Slice[T]) Append(elements ...T) Slice[T]

Append returns a new goo.Slice object with the input elements appended at the end Not yet tested if type is a slice or map

func (Slice[T]) ForEach ΒΆ

func (s Slice[T]) ForEach(exec func(T))

ForEach loop through each element and perform an action with the given function

func (Slice[T]) Length ΒΆ

func (s Slice[T]) Length() int

Length returns the builtin length of slice

func (Slice[T]) ToSlice ΒΆ

func (s Slice[T]) ToSlice() []T

ToSlice converts goo.Slice object to builtin slice of any time

func (Slice[T]) Unique ΒΆ

func (s Slice[T]) Unique() Slice[T]

Unique return unique values from a list Does not work for map, slice, function as it is shallow checking and they will always have different pointers in a slice

type String ΒΆ

type String string

func FromString ΒΆ

func FromString(n string) String

FromString converts builtin type to Goo's string

func (String) Atoi ΒΆ

func (s String) Atoi() (Int, error)

Atoi converts current String to Integer

func (String) HasAnyPrefix ΒΆ

func (s String) HasAnyPrefix(prefixes ...string) Bool

HasAnyPrefix check if the string starts with input

func (String) HasPrefix ΒΆ

func (s String) HasPrefix(prefix string) Bool

HasPrefix check if the string starts with input

func (String) Length ΒΆ

func (s String) Length() int

Length will return builtin int type

func (String) Split ΒΆ

func (s String) Split(sep String) []String

Split will split the current string by the given token and return a slice of String

func (String) ToLower ΒΆ

func (s String) ToLower() String

ToLower will convert all characters of the current String to lowercase

func (String) ToString ΒΆ

func (n String) ToString() string

ToString converts instance to builtin string

func (String) ToUpper ΒΆ

func (s String) ToUpper() String

ToUpper will convert all characters of the current String to uppercase

func (String) Trim ΒΆ

func (s String) Trim(cutset String) String

Trim will remove the given characters from the String instance

type Uint ΒΆ

type Uint uint

func FromUint ΒΆ

func FromUint(n uint) Uint

FromUint converts builtin type to Goo's uint

func (Uint) ToUint ΒΆ

func (n Uint) ToUint() uint

ToUint converts instance to builtin uint

type Uint16 ΒΆ

type Uint16 uint16

func FromUint16 ΒΆ

func FromUint16(n uint16) Uint16

FromUint16 converts builtin type to Goo's uint16

func (Uint16) ToUint16 ΒΆ

func (n Uint16) ToUint16() uint16

ToUint16 converts instance to builtin uint16

type Uint32 ΒΆ

type Uint32 uint32

func FromUint32 ΒΆ

func FromUint32(n uint32) Uint32

FromUint32 converts builtin type to Goo's uint32

func (Uint32) ToUint32 ΒΆ

func (n Uint32) ToUint32() uint32

ToUint32 converts instance to builtin uint32

type Uint64 ΒΆ

type Uint64 uint64

func FromUint64 ΒΆ

func FromUint64(n uint64) Uint64

FromUint64 converts builtin type to Goo's uint64

func (Uint64) ToUint64 ΒΆ

func (n Uint64) ToUint64() uint64

ToUint64 converts instance to builtin uint64

type Uint8 ΒΆ

type Uint8 uint8

func FromUint8 ΒΆ

func FromUint8(n uint8) Uint8

FromUint8 converts builtin type to Goo's uint8

func (Uint8) ToUint8 ΒΆ

func (n Uint8) ToUint8() uint8

ToUint8 converts instance to builtin uint8

type Uintptr ΒΆ

type Uintptr uintptr

func FromUintptr ΒΆ

func FromUintptr(n uintptr) Uintptr

FromUintptr converts builtin type to Goo's uintptr

func (Uintptr) ToUintptr ΒΆ

func (n Uintptr) ToUintptr() uintptr

ToUintptr converts instance to builtin uintptr

Directories ΒΆ

Path Synopsis

Jump to

Keyboard shortcuts

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