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.4k Golang : Find duplicate files with filepath.Walk
+8.7k Golang : How to use Gorilla webtoolkit context package properly
+16.4k Golang : Get the IPv4 and IPv6 addresses for a specific network interface
+6.8k Golang : A simple forex opportunities scanner
+25.3k Golang : Convert IP address string to long ( unsigned 32-bit integer )
+4.9k Golang : The Tao of importing package
+5.3k Golang : Frobnicate or tweaking a string example
+9.3k Golang : Eroding and dilating image with OpenCV example
+5.9k Linux/Unix : Commands that you need to be careful about
+42.6k Golang : Get hardware information such as disk, memory and CPU usage
+33.3k Golang : How to check if slice or array is empty?
+32.7k Delete a directory in Go