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
+8.9k Golang : Get final balance from bit coin address example
+11.2k Golang : How to determine a prime number?
+11.2k Golang : Read until certain character to break for loop
+14.1k Golang : Fix cannot use buffer (type bytes.Buffer) as type io.Writer(Write method has pointer receiver) error
+7.5k Golang : Convert source code to assembly language
+20.8k Golang : Secure(TLS) connection between server and client
+16.4k Golang : Find out mime type from bytes in buffer
+17.1k Golang : How to save log messages to file?
+15.3k Golang : Get timezone offset from date or timestamp
+36.1k Golang : Get file last modified date and time
+8.2k Golang : Randomize letters from a string example
+8.3k Golang : Oanda bot with Telegram and RSI example