Golang : Underscore or snake_case to camel case example
A quick example on how to convert underscore or snake_case
statement to camel case. Pretty much similar to the previous tutorial on how to transform spinal case to pascal case.
Here you go!
package main
import (
"fmt"
"strings"
)
func snakeCaseToCamelCase(inputUnderScoreStr string) (camelCase string) {
//snake_case to camelCase
isToUpper := false
for k, v := range inputUnderScoreStr {
if k == 0 {
camelCase = strings.ToUpper(string(inputUnderScoreStr[0]))
} else {
if isToUpper {
camelCase += strings.ToUpper(string(v))
isToUpper = false
} else {
if v == '_' {
isToUpper = true
} else {
camelCase += string(v)
}
}
}
}
return
}
func main() {
snakeCase := "this_is_a_statement_with_underscore_which_is_also_known_as_Snake_Case"
result := snakeCaseToCamelCase(snakeCase)
fmt.Println(snakeCase)
fmt.Println(result)
}
Output:
this_is_a_statement_with_underscore_which_is_also_known_as_Snake_Case
ThisIsAStatementWithUnderscoreWhichIsAlsoKnownAsSnakeCase
See also : Golang : Transform lisp or spinal case to Pascal case example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+14.7k Golang : Adding XML attributes to xml data or use attribute to differentiate a common tag name
+6.5k Golang : Warp text string by number of characters or runes example
+6k PageSpeed : Clear or flush cache on web server
+11.5k Android Studio : Create custom icons for your application example
+14.7k Golang : Normalize unicode strings for comparison purpose
+37.4k Upload multiple files with Go
+6.8k Golang : Pat multiplexer routing example
+10k Golang : Setting variable value with ldflags
+15.3k Golang : Get all local users and print out their home directory, description and group id
+8.4k Golang : Ackermann function example
+11.7k Golang : convert(cast) float to string
+11.4k Golang : Handle API query by curl with Gorilla Queries example