Last active
November 11, 2022 02:27
-
-
Save nidhi-canopas/2dea9034499ae6f37572f91c73e4c13a to your computer and use it in GitHub Desktop.
A bunch of commonly used function with slice, string and time using Golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math/rand" | |
"regexp" | |
"strconv" | |
"strings" | |
"time" | |
) | |
func main() { | |
// 1. Check whether an element exists in the array or not | |
slice := []string{"apple", "grapes", "mango"} | |
if Contains(slice, "mango") { | |
fmt.Println("Slice contains mango") | |
} else { | |
fmt.Println("Slice doesn't contain mango") | |
} | |
// 2. Check if the given time belongs between two timestamps or not | |
currentTime := time.Now() | |
futureTime := time.Now().Add(time.Hour * 18) | |
intermediateTime := time.Now().Add(time.Hour * 20) | |
if intermediateTime.After(currentTime) && intermediateTime.Before(futureTime) { | |
fmt.Println("intermediateTime is between currentTime and futureTime") | |
} else { | |
fmt.Println("intermediateTime is not inbetween currentTime and futureTime") | |
} | |
// 3. Find the current timestamp of a specific timezone | |
timeZone := "Asia/Shanghai" | |
loc, _ := time.LoadLocation(timeZone) | |
currentTime = time.Now().In(loc) | |
fmt.Println("currentTime : ", currentTime) | |
// 4. Divide a smaller number with a larger one | |
smallerNo := 5 | |
largerNo := 25 | |
quotient := float32(smallerNo) / float32(largerNo) | |
fmt.Println("quotient : ", quotient) | |
// 5. Remove duplicates from an array | |
array := []string{"India", "US", "Canada", "UK"} | |
Shuffle(array) | |
// 6. How to shuffle an array | |
fruits := []string{"Mango", "Grapes", "Kiwi", "Apple", "Grapes"} | |
dulicatesRemovedArray := RemoveDuplicatesFromSlice(fruits) | |
fmt.Println("Array before removing duplicates : ", fruits) | |
fmt.Println("Array after removing duplicates : ", dulicatesRemovedArray) | |
// 7. Reverse an array | |
a := []int{1, 2, 3, 4, 5, 6} | |
reverseArray := ReverseSlice(a) | |
fmt.Println("Reverted array : ", reverseArray) | |
// 8. Sum slice elements | |
s := []int{10, 20, 30} | |
sum := sumSlice(s) | |
fmt.Println("Sum of slice elements : ", sum) | |
// 9. Convert a slice to a comma-separated string | |
result := ConvertSliceToString([]int{10, 20, 30, 40}) | |
fmt.Println("Slice converted string : ", result) | |
// 10. Convert given string to snake_case | |
snakeCase := ConvertToSnakeCase("ILike#ProgrammingINGo123") | |
fmt.Println("String in snake case : ", snakeCase) | |
} | |
func Contains(slice []string, element string) bool { | |
for _, i := range slice { | |
if i == element { | |
return true | |
} | |
} | |
return false | |
} | |
func Shuffle(array []string) { | |
random := rand.New(rand.NewSource(time.Now().UnixNano())) | |
for i := len(array) - 1; i > 0; i-- { | |
j := random.Intn(i + 1) | |
array[i], array[j] = array[j], array[i] | |
} | |
fmt.Println("Shuffled array : ", array) | |
} | |
func RemoveDuplicatesFromSlice(intSlice []string) []string { | |
keys := make(map[string]bool) | |
list := []string{} | |
for _, entry := range intSlice { | |
if _, value := keys[entry]; !value { | |
keys[entry] = true | |
list = append(list, entry) | |
} | |
} | |
return list | |
} | |
func ReverseSlice(a []int) []int { | |
for i := len(a)/2 - 1; i >= 0; i-- { | |
pos := len(a) - 1 - i | |
a[i], a[pos] = a[pos], a[i] | |
} | |
return a | |
} | |
func sumSlice(array []int) int { | |
sum := 0 | |
for _, item := range array { | |
sum += item | |
} | |
return sum | |
} | |
func ConvertSliceToString(input []int) string { | |
var output []string | |
for _, i := range input { | |
output = append(output, strconv.Itoa(i)) | |
} | |
return strings.Join(output, ",") | |
} | |
func ConvertToSnakeCase(input string) string { | |
var matchChars = regexp.MustCompile("(.)([A-Z][a-z]+)") | |
var matchAlpha = regexp.MustCompile("([a-z0-9])([A-Z])") | |
snake := matchChars.ReplaceAllString(input, "${1}_${2}") | |
snake = matchAlpha.ReplaceAllString(snake, "${1}_${2}") | |
return strings.ToLower(snake) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment