diff --git a/15-sorting/custom-sorting-struct.go b/15-sorting/custom-sorting-struct.go new file mode 100644 index 0000000..1c69e21 --- /dev/null +++ b/15-sorting/custom-sorting-struct.go @@ -0,0 +1,57 @@ +package main + +import ( + "fmt" + "sort" +) + +type User struct { + Name string + Age int +} + +type UsersByAge []User + +func (u UsersByAge) Len() int { + return len(u) +} +func (u UsersByAge) Swap(i, j int) { + u[i], u[j] = u[j], u[i] +} +func (u UsersByAge) Less(i, j int) bool { + return u[i].Age < u[j].Age +} + +func main() { + users := []User{ + { + Name: "Rajeev", + Age: 28, + }, + { + Name: "Monica", + Age: 31, + }, + { + Name: "John", + Age: 56, + }, + { + Name: "Amanda", + Age: 16, + }, + { + Name: "Steven", + Age: 28, + }, + } + + // Sorting a slice of users by age + sort.Sort(UsersByAge(users)) + fmt.Println("Sorted users by age: ", users) + + // Sorting a slice of strings in the reverse order of length + sort.Stable(UsersByAge(users)) + fmt.Println("[Stable] Sorted users by age: ", users) + +} diff --git a/15-sorting/reverse-sorting.go b/15-sorting/reverse-sorting.go new file mode 100644 index 0000000..cad0ad5 --- /dev/null +++ b/15-sorting/reverse-sorting.go @@ -0,0 +1,23 @@ +package main + +import ( + "fmt" + "sort" +) + +func main() { + // Sorting a slice of Strings + strs := []string{"quick", "brown", "fox", "jumpes"} + sort.Sort(sort.Reverse(sort.StringSlice(strs))) + fmt.Println("Sorted strings in reverse order: ", strs) + + // Sorting a slice of Integers + ints := []int{56, 19, 78, 67, 14, 25} + sort.Sort(sort.Reverse(sort.IntSlice(ints))) + fmt.Println("Sorted integers in reverse order: ", ints) + + // Sorting a slice of Floats + floats := []float64{176.8, 19.5, 20.8, 57.4} + sort.Sort(sort.Reverse(sort.Float64Slice(floats))) + fmt.Println("Sorted floats in reverse order: ", floats) +} diff --git a/15-sorting/sorting-by-comparator.go b/15-sorting/sorting-by-comparator.go new file mode 100644 index 0000000..4f8f97c --- /dev/null +++ b/15-sorting/sorting-by-comparator.go @@ -0,0 +1,27 @@ +package main + +import ( + "fmt" + "sort" +) + +func main() { + // Sorting a slice of strings by length + strs := []string{"United States", "India", "France", "United Kingdom", "Spain"} + sort.Slice(strs, func(i, j int) bool { + return len(strs[i]) < len(strs[j]) + }) + fmt.Println("Sorted strings by length: ", strs) + + // Stable sort + sort.SliceStable(strs, func(i, j int) bool { + return len(strs[i]) < len(strs[j]) + }) + fmt.Println("[Stable] Sorted strings by length: ", strs) + + // Sorting a slice of strings in the reverse order of length + sort.SliceStable(strs, func(i, j int) bool { + return len(strs[j]) < len(strs[i]) + }) + fmt.Println("[Stable] Sorted strings by reverse order of length: ", strs) +} diff --git a/15-sorting/sorting-struct-slice-by-comparator.go b/15-sorting/sorting-struct-slice-by-comparator.go new file mode 100644 index 0000000..a233d74 --- /dev/null +++ b/15-sorting/sorting-struct-slice-by-comparator.go @@ -0,0 +1,48 @@ +package main + +import ( + "fmt" + "sort" +) + +type User struct { + Name string + Age int +} + +func main() { + // Sorting a slice of structs by a field + users := []User{ + { + Name: "Rajeev", + Age: 28, + }, + { + Name: "Monica", + Age: 31, + }, + { + Name: "John", + Age: 56, + }, + { + Name: "Amanda", + Age: 16, + }, + { + Name: "Steven", + Age: 28, + }, + } + + sort.Slice(users, func(i, j int) bool { + return users[i].Age < users[j].Age + }) + fmt.Println("Sorted users by age: ", users) + + // Stable sort + sort.SliceStable(users, func(i, j int) bool { + return users[i].Age < users[j].Age + }) + fmt.Println("Sorted users by age: ", users) +} diff --git a/15-sorting/sorting.go b/15-sorting/sorting.go new file mode 100644 index 0000000..61a8235 --- /dev/null +++ b/15-sorting/sorting.go @@ -0,0 +1,23 @@ +package main + +import ( + "fmt" + "sort" +) + +func main() { + // Sorting a slice of Strings + strs := []string{"quick", "brown", "fox", "jumpes"} + sort.Strings(strs) + fmt.Println("Sorted strings: ", strs) + + // Sorting a slice of Integers + ints := []int{56, 19, 78, 67, 14, 25} + sort.Ints(ints) + fmt.Println("Sorted integers: ", ints) + + // Sorting a slice of Floats + floats := []float64{176.8, 19.5, 20.8, 57.4} + sort.Float64s(floats) + fmt.Println("Sorted floats: ", floats) +} diff --git a/16-command-line-flags/command-line-flags.go b/16-command-line-flags/command-line-flags.go new file mode 100644 index 0000000..359ee53 --- /dev/null +++ b/16-command-line-flags/command-line-flags.go @@ -0,0 +1,56 @@ +package main + +import ( + "flag" + "fmt" + "strings" +) + +type hobbies []string + +func (h *hobbies) String() string { + return fmt.Sprint(*h) +} + +func (h *hobbies) Set(value string) error { + for _, hobby := range strings.Split(value, ",") { + *h = append(*h, hobby) + } + return nil +} + +func main() { + // Declare a string flag called name with a default value ("Guest") and a help message + name := flag.String("name", "Guest", "specify your name") + + // Declare a flag called age with default value of 0 and a help message + age := flag.Int("age", 0, "specify your age") + + // Bind the command-line flag with an existing variable + var country string + flag.StringVar(&country, "country", "", "enter your country") + + // Defining a custom flag + var hobbiesFlag hobbies + flag.Var(&hobbiesFlag, "hobbies", "comma separated list of hobbies") + + // Enable command-line parsing + flag.Parse() + + fmt.Printf("Hello %s\n", *name) + fmt.Printf("Your age is %d\n", *age) + fmt.Printf("Your are from %s\n", country) + + fmt.Printf("Your hobbies are: ") + for _, hobby := range hobbiesFlag { + fmt.Printf("%s ", hobby) + } + fmt.Println() + + // Read all the positional arguments + fmt.Println("Positional Arguments:", flag.Args()) + + // Read the i'th positional argument + i := 0 + fmt.Printf("Positional argument at index %d: %v\n", i, flag.Arg(i)) +} diff --git a/16-command-line-flags/positional-arguments.go b/16-command-line-flags/positional-arguments.go new file mode 100644 index 0000000..4c01a46 --- /dev/null +++ b/16-command-line-flags/positional-arguments.go @@ -0,0 +1,23 @@ +package main + +import ( + "flag" + "fmt" +) + +func main() { + s := flag.Int("s", 0, "start line number") + t := flag.Int("t", 0, "end line number") + + // Enable command-line parsing + flag.Parse() + + fmt.Printf("Search file from line number %d to %d\n", *s, *t) + + // Read all the positional arguments + fmt.Println("for keywords:", flag.Args()) + + // Read the i'th positional argument + i := 0 + fmt.Printf("The keyword at index %d: %v\n", i, flag.Arg(i)) +} diff --git a/17-url-encoding-decoding/01-url-encode/main.go b/17-url-encoding-decoding/01-url-encode/main.go new file mode 100644 index 0000000..9be52a7 --- /dev/null +++ b/17-url-encoding-decoding/01-url-encode/main.go @@ -0,0 +1,19 @@ +package main + +import ( + "fmt" + "net/url" +) + +func main() { + // QueryEscape: Escape a string to safely place it inside a URL query string + str := "Gol@ng?&" + encodedStr := url.QueryEscape(str) + + fmt.Println(encodedStr) + + // PathEscape: Escape a string to safely place it inside a URL path segment + pathVar := "Gol@ng?&" + encodedPathVar := url.PathEscape(pathVar) + fmt.Println(encodedPathVar) +} diff --git a/17-url-encoding-decoding/02-url-decode/main.go b/17-url-encoding-decoding/02-url-decode/main.go new file mode 100644 index 0000000..a6bbbfb --- /dev/null +++ b/17-url-encoding-decoding/02-url-decode/main.go @@ -0,0 +1,25 @@ +package main + +import ( + "fmt" + "net/url" +) + +func main() { + // QueryUnescape: Decode a URL query string + encodedStr := "Gol%40ng%3F%26" + decodedStr, err := url.QueryUnescape(encodedStr) + if err != nil { + fmt.Printf("Error decoding the string %v", err) + } + + fmt.Println(decodedStr) + + // PathUnescape: Decode a URL path segment + encodedPathVar := "Gol@ng%3F&" + decodedPathVar, err := url.PathUnescape(encodedPathVar) + if err != nil { + fmt.Printf("Error decoding the string %v", err) + } + fmt.Println(decodedPathVar) +} diff --git a/18-base64-encoding-decoding/01-base64-encoding/main.go b/18-base64-encoding-decoding/01-base64-encoding/main.go new file mode 100644 index 0000000..a83a8e9 --- /dev/null +++ b/18-base64-encoding-decoding/01-base64-encoding/main.go @@ -0,0 +1,27 @@ +package main + +import ( + "encoding/base64" + "fmt" +) + +func main() { + data := "Gol@ng is Awesome?~" + + // Standard Base64 Encoding + encodedData := base64.StdEncoding.EncodeToString([]byte(data)) + fmt.Println(encodedData) + + // Standard Base64 Encoding without padding + encodedDataWithoutPadding := base64.RawStdEncoding.EncodeToString([]byte(data)) + fmt.Println(encodedDataWithoutPadding) + + // URL and filename-safe Base64 encoding + urlSafeEncodedData := base64.URLEncoding.EncodeToString([]byte(data)) + fmt.Println(urlSafeEncodedData) + + // URL and filename-safe Base64 encoding without padding + urlSafeEncodedDataWithoutPadding := base64.RawURLEncoding.EncodeToString([]byte(data)) + fmt.Println(urlSafeEncodedDataWithoutPadding) + +} diff --git a/18-base64-encoding-decoding/02-base64-decoding/main.go b/18-base64-encoding-decoding/02-base64-decoding/main.go new file mode 100644 index 0000000..e765042 --- /dev/null +++ b/18-base64-encoding-decoding/02-base64-decoding/main.go @@ -0,0 +1,25 @@ +package main + +import ( + "encoding/base64" + "fmt" +) + +func main() { + encodedData := "R29sQG5nIGlzIEF3ZXNvbWU/fg==" + + // Standard Base64 Decoding + decodedData, err := base64.StdEncoding.DecodeString(encodedData) + if err != nil { + fmt.Printf("Error decoding Base64 encoded data %v", err) + } + fmt.Println(string(decodedData)) + + // URL and filename-safe Base64 decoding + urlSafeBase64EncodedData := "R29sQG5nIGlzIEF3ZXNvbWU_fg==" + urlSafeData, err := base64.URLEncoding.DecodeString(urlSafeBase64EncodedData) + if err != nil { + fmt.Printf("Error decoding Base64 encoded data %v", err) + } + fmt.Println(string(urlSafeData)) +}