itcuihao / blog

blog
6 stars 0 forks source link

驼峰转下划线 #640

Open itcuihao opened 4 years ago

itcuihao commented 4 years ago
// 驼峰式写法转为下划线写法
func hump2Underscore(name string) string {
    builder:=strings.Builder{}
    for i, r := range name {
        if unicode.IsUpper(r) {
            if i != 0 {
                builder.WriteByte('_')
            }
            builder.WriteRune(unicode.ToLower(r))
        } else {
            builder.WriteRune(r)
        }
    }
    return builder.String()
}
flyfy1 commented 4 years ago

似乎没有考虑首字母的情况(比如 exported constant)

// test case
BigBrother
itcuihao commented 4 years ago

似乎没有考虑首字母的情况(比如 exported constant)

// test case
BigBrother

是转成 big_brother 这种吗?

flyfy1 commented 4 years ago

嗯 应该是吧 😄想了一下,如果是 exported constant,似乎也不需要做转换

itcuihao commented 4 years ago

嗯 应该是吧 😄想了一下,如果是 exported constant,似乎也不需要做转换

嗯啊,如果是大写就tolower了,请教下 exported constant 是怎么写法呢?我还是第一次听到。

flyfy1 commented 4 years ago

就是在Golang的package定义时候,大写的都是public的,小写的都是private的。如果定义比如

const SomeConfiguration

那么这个 SomeConfiguration 就是一个public variable / const啦