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 🤣!
- 📖 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.
- 🔀 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!
- 🔒 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.
- 📁 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.
- 🌈 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.
- 🔧 Customize and extend: Need something extra? G is your best buddy, ready to be extended or modified to suit any project.
- 📚 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! 🥳
Generate a securely random string.
stdlib |
g |
---|---|
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.
stdlib |
g |
---|---|
func main() {
md := make(map[int][]int)
for i := range 5 {
md[i] = append(md[i], 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))
}
// or
for i := range 5 {
entry := md.Entry(i)
entry.OrDefault() // Insert an empty slice if missing
entry.Transform(
func(s Slice[int]) Slice[int] {
return s.Append(i) // Append the current index to the slice
})
}
fmt.Println(md)
} |
Copy copies the contents of the current directory to the destination directory.
stdlib |
g |
---|---|
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.
stdlib |
g |
---|---|
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)
} |