Converts a string to snake case.
Use strings.Fields()
to get the words in the string, strings.Join()
to combine them using "_"
as the separator.
代码实现
import "strings"
func ToSnake(s string) string {
return strings.Join(strings.Fields(s), "_")
}
使用样例
ToSnake("some text") // "some_text"