Categorygithub.com/enetx/g
modulepackage
1.0.113
Repository: https://github.com/enetx/g.git
Documentation: pkg.go.dev

# README

ðŸĪŠ G: Go Crazy, Go G, Go Nuts!

Go Reference Go Report Card Coverage Status Go

Introducing G, the wackiest Go package on the planet, created to make your coding experience an absolute riot! With G, you can forget about dull and monotonous code, we're all about turning the mundane into the insanely hilarious. It's not just a bicycle, it's almost a motorcycle ðŸĪĢ!

🎉 What's in the box?

  1. 📖 Readable syntax: Boring code is so yesterday! G turns your code into a party by blending seamlessly with Go and making it super clean and laughably maintainable.
  2. 🔀 Encoding and decoding: Juggling data formats? No problemo! G's got your back with Base64, URL, Gzip, and Rot13 support. Encode and decode like a pro!
  3. 🔒 Hashing extravaganza: Safety first, right? Hash your data with MD5, SHA1, SHA256, or SHA512, and enjoy peace of mind while G watches over your bytes.
  4. 📁 File and directory shenanigans: Create, read, write, and dance through files and directories with G's fun-tastic functions. Trust us, managing files has never been this entertaining.
  5. 🌈 Data type compatibility: Strings, integers, floats, bytes, slices, maps, you name it! G is the life of the party, mingling with all your favorite data types.
  6. 🔧 Customize and extend: Need something extra? G is your best buddy, ready to be extended or modified to suit any project.
  7. 📚 Docs & examples: We're not just about fun and games, we've got detailed documentation and examples that'll have you smiling from ear to ear as you learn the G way.

Take your Go projects to a whole new level of excitement with G! It's time to stop coding like it's a chore and start coding like it's a celebration! ðŸĨģ

Examples

Generate a securely random string.

stdlibg
func main() {
	const charset = "abcdefghijklmnopqrstuvwxyz" +
		"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

	length := 10

	b := make([]byte, length)
	if _, err := rand.Read(b); err != nil {
		return
	}

	for i, v := range b {
		b[i] = charset[v%byte(len(charset))]
	}

	result := string(b)
	fmt.Println(result)
}
func main() {
	result := g.NewString().Random(10)
	fmt.Println(result)
}

GetOrDefault returns the value for a key. If the key does not exist, returns the default value instead. This function is useful when you want to provide a fallback value for keys that may not be present in the map.

stdlibg
func main() {
	md := map[int][]int{}

	for i := range 5 {
		value, ok := md[i]
		if !ok {
			value = []int{}
		}

		md[i] = append(value, i)
	}

	fmt.Println(md)
}
func main() {
	md := g.NewMap[int, g.Slice[int]]()

	for i := range 5 {
		md.Set(i, md.Get(i).UnwrapOrDefault().Append(i))
	}
}

Copy copies the contents of the current directory to the destination directory.

stdlibg
func copyDir(src, dest string) error {
	return filepath.Walk(src, func(path string,
		info fs.FileInfo, err error,
	) error {
		if err != nil {
			return err
		}

		relPath, err := filepath.Rel(src, path)
		if err != nil {
			return err
		}

		destPath := filepath.Join(dest, relPath)

		if info.IsDir() {
			return os.MkdirAll(destPath, info.Mode())
		}

		return copyFile(path, destPath, info.Mode())
	})
}

func copyFile(src, dest string, mode fs.FileMode) error {
	srcFile, err := os.Open(src)
	if err != nil {
		return err
	}
	defer srcFile.Close()

	destFile, err := os.OpenFile(dest, os.O_CREATE|os.O_WRONLY, mode)
	if err != nil {
		return err
	}
	defer destFile.Close()

	_, err = io.Copy(destFile, srcFile)

	return err
}

func main() {
	src := "path/to/source/directory"
	dest := "path/to/destination/directory"

	err := copyDir(src, dest)
	if err != nil {
		fmt.Println("Error copying directory:", err)
	} else {
		fmt.Println("Directory copied successfully")
	}
}
func main() {
	g.NewDir(".").Copy("copy").Unwrap()
}

RandomSample returns a new slice containing a random sample of elements from the original slice.

stdlibg
func RandomSample(slice []int, amount int) []int {
	if amount > len(slice) {
		amount = len(slice)
	}

	samples := make([]int, amount)

	for i := 0; i < amount; i++ {
		index, _ := rand.Int(rand.Reader, big.NewInt(int64(len(slice))))
		samples[i] = slice[index.Int64()]
		slice = append(slice[:index.Int64()], slice[index.Int64()+1:]...)
	}

	return samples
}

func main() {
	slice := []int{1, 2, 3, 4, 5, 6}
	samples := RandomSample(slice, 3)
	fmt.Println(samples)
}
func main() {
	slice := g.SliceOf(1, 2, 3, 4, 5, 6)
	samples := slice.RandomSample(3)
	fmt.Println(samples)
}

# Packages

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

# Functions

Err returns a new Result[T] containing the given error.
Format formats a string (str) by replacing placeholders with values from a map (args) and returns the result as a String.
FromChan converts a channel into an iterator.
MapOrdFromStd converts a standard Go map to an ordered Map.
NewBuilder creates a new instance of Builder.
NewBytes creates a new Bytes value.
NewDir returns a new Dir instance with the given path.
NewFile returns a new File instance with the given name.
NewFloat creates a new Float with the provided value.
NewInt creates a new Int with the provided int value.
NewMap creates a new Map of the specified size or an empty Map if no size is provided.
NewMapOrd creates a new ordered Map with the specified size (if provided).
NewMapSafe creates a new instance of MapSafe with an optional initial size.
NewPool[T any] creates a new goroutine pool.
NewSet creates a new Set of the specified size or an empty Set if no size is provided.
NewSlice creates a new Slice of the given generic type T with the specified length and capacity.
NewString creates a new String from the provided string.
None creates an Option containing a nil value.
Ok returns a new Result[T] containing the given value.
OptionOf creates an Option[T] based on the provided value and status flag.
ResultOf returns a new Result[T] based on the provided value and error.
SetOf creates a new generic set containing the provided elements.
SliceOf creates a new generic slice containing the provided elements.
Some creates an Option containing a non-nil value.
Sprint formats using the default formats for its operands and returns the resulting String.
Sprintf formats according to a format specifier and returns the resulting String.
No description provided by the author
TransformOption applies the given function to the value inside the Option, producing a new Option with the transformed value.
TransformResult applies the given function to the value inside the Result, producing a new Result with the transformed value.
TransformResultOf applies the given function to the value inside the Result, producing a new Result with the transformed value.
TransformSet applies the given function to each element of a Set and returns a new Set containing the transformed values.
TransformSlice applies the given function to each element of a Slice and returns a new Slice containing the transformed values.

# Constants

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

# Variables

SkipWalk is used as a return value from the walker function to indicate that the file or directory named in the call should be skipped.
StopWalk is used as a return value from the walker function to indicate that all remaining files and directories should be skipped.

# Structs

Builder represents a string builder.
No description provided by the author
ErrFileClosed represents an error for when a file is already closed.
ErrFileNotExist represents an error for when a file does not exist.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Type aliases

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