Skip to content

Instantly share code, notes, and snippets.

@nidhi-canopas
Created February 22, 2022 05:50
Show Gist options
  • Select an option

  • Save nidhi-canopas/17a68b3c36cc2f4c970f887773265d29 to your computer and use it in GitHub Desktop.

Select an option

Save nidhi-canopas/17a68b3c36cc2f4c970f887773265d29 to your computer and use it in GitHub Desktop.
import (
"fmt"
"strings"
"regexp"
)
func main() {
snakeCase := ConvertToSnakeCase("ILikeProgrammingINGo123")
fmt.Println("String in snake case : ", snakeCase)
}
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)
}
output:
String in snake case : i_like_programming_in_go123
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment