2017-05-04 19:44
浏览 356


I really want a way to print the string representation of a field name in go. It has several use cases, but here is an example:

lets say I have a struct

type Test struct {
    Field      string `bson:"Field" json:"field"`
    OtherField int    `bson:"OtherField" json:"otherField"`

and, for example, I want to do a mongo find:

collection.Find(bson.M{"OtherField": someValue})

I don't like that I have to put the string "OtherField" in there. It seems brittle and easy to either misstype or have the struct change and then my query fails without me knowing it.

Is there any way to get the string "OtherField" without having to either declare a const or something like that? I know I can use reflection to a get a list of field names from a struct, but I'd really like to do something along the lines of

fieldName := nameOf(Test{}.OtherField) 
collection.Find(bson.M{fieldName: someValue})

is there any way to do this in Go?? C# 6 has the built in nameof, but digging through reflection I can't find any way to do this in Go.

图片转代码服务由CSDN问答提供 功能建议

我真的想要一种在go中打印字段名称的字符串表示形式的方法。 它有几种用例,但这是一个示例:


字段 字符串`bson:“ Field” json:“ field”`
 OtherField int`bson:“ OtherField” json:“ otherField”`

和 ,例如,我想进行mongo查找:

  collection.Find(bson.M {“ OtherField”:someValue})

我不喜欢这样,我必须在其中放置字符串“ OtherField”。 丢失类型或更改结构似乎很脆弱且容易,然后我的查询失败,而我却不知道。

有什么方法可以获取字符串“ OtherField”而不必声明const或类似的东西吗? 我知道我可以使用反射从结构中获取字段名称列表,但是我真的很想按照

  fieldName:= nameOf(  Test {}。OtherField)
collection.Find(bson.M {fieldName:someValue})

有没有办法在Go中做到这一点? C#6具有内置的nameof,但是通过反射进行挖掘,我找不到在Go中执行此操作的任何方法。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douzhicai7148 2017-05-04 19:54

    I don't really think there is. You may be able to load a set of types via reflection and generate a set of constants for the field names. So:

    type Test struct {
        Field      string `bson:"Field" json:"field"`
        OtherField int    `bson:"OtherField" json:"otherField"`

    Could generate something like:

    var TestFields = struct{
          Field string
          OtherField string

    and you could use TestFields.Field as a constant.

    Unfortunately, I don't know of any existing tool that does anything like that. Would be fairly simple to do, and wire up to go generate though.


    How I'd generate it:

    1. Make a package that accepts an array of reflect.Type or interface{} and spits out a code file.
    2. Make a generate.go somewhere in my repo with main function:

      func main(){
         var text = mygenerator.Gen(Test{}, OtherStruct{}, ...)
         // write text to constants.go or something
    3. Add //go:generate go run scripts/generate.go to my main app and run go generate
    解决 无用
    打赏 举报
  • 查看更多回答(1条)

相关推荐 更多相似问题