Skip to content

Instantly share code, notes, and snippets.

@mdouchement
Created November 16, 2017 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdouchement/c536e46a501caf134b5415ae69fa4996 to your computer and use it in GitHub Desktop.
Save mdouchement/c536e46a501caf134b5415ae69fa4996 to your computer and use it in GitHub Desktop.
Golang camelcase / camelize
package main
import (
"fmt"
"unicode"
)
func main() {
for _, v := range []string{"target_prob1", "target_prob_1", "_target_prob_1", "target_prob_1_"} {
fmt.Printf("%s => %s\n", v, ToCamel(v))
}
}
// ToCamel camelcases the given string.
func ToCamel(in string) string {
runes := []rune(in)
var out []rune
for i, r := range runes {
if r == '_' {
continue
}
if i == 0 || runes[i-1] == '_' {
out = append(out, unicode.ToUpper(r))
continue
}
out = append(out, r)
}
return string(out)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment