Created
November 16, 2017 16:58
-
-
Save mdouchement/c536e46a501caf134b5415ae69fa4996 to your computer and use it in GitHub Desktop.
Golang camelcase / camelize
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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