如何标记无法修改的嵌入式结构的字段?

Suppose there is an external library libA who declares NotMyType.

type NotMyType struct {
  NotMyField string
}

And you would like to embed it with one of your own types, which you use with an ORM, which uses the tags to adjust column properties.

type MyType struct {
  SomeData0 string  `orm:"nullable"`
  SomeData1 string  `orm:"nullable"`
  libA.NotMyType
}

For example, columns MyType.SomeData0 and MyType.SomeData1 are NULLABLE.

Is it possible to tag NotMyField with orm:"nullable" without modifying NotMyType?

EDIT: wrong answer, my bad

Okep i think i got it. I have never used reflect before so maybe i'm wrong. But :

Take a look at what I just do :

https://play.golang.org/p/U0uyomL-It

I have a struct

type User struct {
    name string
    age  int
}

With no tag.

I get the field with

field, ok := reflect.TypeOf(user).Elem().FieldByName("name")
if !ok {
    panic("Field not found")
}

And i set a new tag with

//setStructTag(&field)

func setStructTag(f *reflect.StructField) {
    f.Tag = "`json:name-field`"
}

Hope my researche will help you :)

Go is introspective. It has introspection (e.g. find-class allows to look at existing classes) but no intercession (e.g. ensure-class creates/modifies a class at runtime).

So you can't.