我想知道是否有一种本地方法来转换字符串,例如:
a.string
a-string
a_string
a string
在遵循结构公共字段成员约定的字符串中,在 Go 中。
这个想法是编写一个接受字符串的函数并尝试获取字段的值,即使传递的字符串没有使用 PascalCase 约定,例如:
type MyStruct struct {
Debug bool
AString bool
SomethingMoreComplex
}
var myStruct MyStruct
func GetField(s string) reflect.Value {
v := reflect.ValueOf(myStruct)
return v.FieldByName(s)
}
function main() {
GetField("debug")
GetField("a.string")
GetField("a-string")
GetField("a_string")
GetField("-a.string")
GetField("something-more-complex")
}
我使用的是 strcase 包,但它只适用于 ASCII。