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
+19.8k nginx: [emerg] unknown directive "passenger_enabled"
+24.7k Golang : Convert long hexadecimal with strconv.ParseUint example
+14k Golang : Reset buffer example
+10.1k Golang : Resolve domain name to IP4 and IP6 addresses.
+11.8k Golang : Display list of countries and ISO codes
+9.8k Golang : How to profile or log time spend on execution?
+6.9k Golang : Accessing dataframe-go element by row, column and name example
+35.5k Golang : How to split or chunking a file to smaller pieces?
+21.6k Golang : How to run Golang application such as web server in the background or as daemon?
+4.3k MariaDB/MySQL : How to get version information
+13k Golang : Get user input until a command or receive a word to stop
+10.6k How to test Facebook App on localhost ?