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
+7.6k Golang : Lock executable to a specific machine with unique hash of the machine
+8.1k Golang : Emulate NumPy way of creating matrix example
+7.3k Golang : Gorrila set route name and get the current route name
+4.4k Java : Generate multiplication table example
+13.8k Golang: Pad right or print ending(suffix) zero or spaces in fmt.Printf example
+26.4k Golang : Encrypt and decrypt data with AES crypto
+18.3k Golang : Write file with io.WriteString
+9.5k Javascript : Read/parse JSON data from HTTP response
+6.4k Golang : Map within a map example
+12.8k Swift : Convert (cast) Int to String ?
+11.9k Golang : Detect user location with HTML5 geo-location
+12.5k Golang : Transform comma separated string to slice example