github.com/enetx/g@v1.0.80/README.md (about) 1 <p align="center"> 2 <img src="https://user-images.githubusercontent.com/65846651/229838021-741ff719-8c99-45f6-88d2-1a32927bd863.png"> 3 </p> 4 5 # ðĪŠ G: Go Crazy, Go G, Go Nuts! 6 [![Go Reference](https://pkg.go.dev/badge/github.com/enetx/g.svg)](https://pkg.go.dev/github.com/enetx/g) 7 [![Go Report Card](https://goreportcard.com/badge/github.com/enetx/g)](https://goreportcard.com/report/github.com/enetx/g) 8 [![Coverage Status](https://coveralls.io/repos/github/enetx/g/badge.svg?branch=main&service=github)](https://coveralls.io/github/enetx/g?branch=main) 9 [![Go](https://github.com/enetx/g/actions/workflows/go.yml/badge.svg)](https://github.com/enetx/g/actions/workflows/go.yml) 10 11 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 ðĪĢ! 12 13 ## ð What's in the box? 14 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. 15 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! 16 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. 17 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. 18 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. 19 6. ð§ **Customize and extend:** Need something extra? G is your best buddy, ready to be extended or modified to suit any project. 20 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. 21 22 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! ðĨģ 23 24 # Examples 25 26 Generate a securely random string. 27 28 <table> 29 <tr> 30 <th><code>stdlib</code></th> 31 <th><code>g</code></th> 32 </tr> 33 <tr> 34 <td> 35 36 ```go 37 func main() { 38 const charset = "abcdefghijklmnopqrstuvwxyz" + 39 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" 40 41 length := 10 42 43 b := make([]byte, length) 44 if _, err := rand.Read(b); err != nil { 45 return 46 } 47 48 for i, v := range b { 49 b[i] = charset[v%byte(len(charset))] 50 } 51 52 result := string(b) 53 fmt.Println(result) 54 } 55 ``` 56 </td> 57 <td> 58 59 ```go 60 func main() { 61 result := g.NewString().Random(10) 62 fmt.Println(result) 63 } 64 ``` 65 </td> 66 </tr> 67 </table> 68 69 GetOrDefault returns the value for a key. If the key does not exist, returns the default value 70 instead. This function is useful when you want to provide a fallback value for keys that may not 71 be present in the map. 72 73 <table> 74 <tr> 75 <th><code>stdlib</code></th> 76 <th><code>g</code></th> 77 </tr> 78 <tr> 79 <td> 80 81 ```go 82 func main() { 83 md := map[int][]int{} 84 85 for i := range 5 { 86 value, ok := md[i] 87 if !ok { 88 value = []int{} 89 } 90 91 md[i] = append(value, i) 92 } 93 94 fmt.Println(md) 95 } 96 ``` 97 </td> 98 <td> 99 100 ```go 101 func main() { 102 md := g.NewMap[int, g.Slice[int]]() 103 104 for i := range 5 { 105 md.Set(i, md.Get(i).UnwrapOrDefault().Append(i)) 106 } 107 } 108 ``` 109 </td> 110 </tr> 111 </table> 112 113 Copy copies the contents of the current directory to the destination directory. 114 115 <table> 116 <tr> 117 <th><code>stdlib</code></th> 118 <th><code>g</code></th> 119 </tr> 120 <tr> 121 <td> 122 123 ```go 124 func copyDir(src, dest string) error { 125 return filepath.Walk(src, func(path string, 126 info fs.FileInfo, err error, 127 ) error { 128 if err != nil { 129 return err 130 } 131 132 relPath, err := filepath.Rel(src, path) 133 if err != nil { 134 return err 135 } 136 137 destPath := filepath.Join(dest, relPath) 138 139 if info.IsDir() { 140 return os.MkdirAll(destPath, info.Mode()) 141 } 142 143 return copyFile(path, destPath, info.Mode()) 144 }) 145 } 146 147 func copyFile(src, dest string, mode fs.FileMode) error { 148 srcFile, err := os.Open(src) 149 if err != nil { 150 return err 151 } 152 defer srcFile.Close() 153 154 destFile, err := os.OpenFile(dest, os.O_CREATE|os.O_WRONLY, mode) 155 if err != nil { 156 return err 157 } 158 defer destFile.Close() 159 160 _, err = io.Copy(destFile, srcFile) 161 162 return err 163 } 164 165 func main() { 166 src := "path/to/source/directory" 167 dest := "path/to/destination/directory" 168 169 err := copyDir(src, dest) 170 if err != nil { 171 fmt.Println("Error copying directory:", err) 172 } else { 173 fmt.Println("Directory copied successfully") 174 } 175 } 176 ``` 177 </td> 178 <td> 179 180 ```go 181 func main() { 182 g.NewDir(".").Copy("copy").Unwrap() 183 } 184 ``` 185 </td> 186 </tr> 187 </table> 188 189 RandomSample returns a new slice containing a random sample of elements from the original slice. 190 191 <table> 192 <tr> 193 <th><code>stdlib</code></th> 194 <th><code>g</code></th> 195 </tr> 196 <tr> 197 <td> 198 199 ```go 200 func RandomSample(slice []int, amount int) []int { 201 if amount > len(slice) { 202 amount = len(slice) 203 } 204 205 samples := make([]int, amount) 206 207 for i := 0; i < amount; i++ { 208 index, _ := rand.Int(rand.Reader, big.NewInt(int64(len(slice)))) 209 samples[i] = slice[index.Int64()] 210 slice = append(slice[:index.Int64()], slice[index.Int64()+1:]...) 211 } 212 213 return samples 214 } 215 216 func main() { 217 slice := []int{1, 2, 3, 4, 5, 6} 218 samples := RandomSample(slice, 3) 219 fmt.Println(samples) 220 } 221 ``` 222 </td> 223 <td> 224 225 ```go 226 func main() { 227 slice := g.SliceOf(1, 2, 3, 4, 5, 6) 228 samples := slice.RandomSample(3) 229 fmt.Println(samples) 230 } 231 ``` 232 </td> 233 </tr> 234 </table> 235 236 ## Requires GOEXPERIMENT=rangefunc.