Is it possible to give type name to dynamic creation struct from reflect.StructOf

I have created struct from reflect.StructOf dynamically, but when I get the new type name with the Value.Type(), it’s always return empty string “”.
Is it possible to assign or give name to the new type? Thanks.

Please show us a short but complete example of code that demonstrates this.

package main                                                                                                                                                            
                                                                                                                                                                        
import (                                                                                                                                                                
   "fmt"                                                                                                                                                                
   "reflect"                                                                                                                                                            
   "strings"                                                                                                                                                            
)  
type foo struct{                                                                                                                                                        
   Name string `toberepaced: "name"`                                                                                                                                    
}                                                                                                                                                                       
                                                                                                                                                                        
func (f foo) Bar() string {                                                                                                                                             
   return "bar"                                                                                                                                                         
}                                                                                                                                                                       
                                                                                                                                                                        
func main() {                                                                                                                                                           
   obj1 := foo{Name: "test"}                                                                                                                                            
   st := reflect.TypeOf(obj1)                                                                                                                                           
   new := []reflect.StructField{}                                                                                                                                       
                                                                                                                                                                        
   for i := 0; i < st.NumField(); i++ {                                                                                                                                 
      field := st.Field(i)                                                                                                                                              
      field.Tag = reflect.StructTag(strings.Replace(string(field.Tag), "toberepaced", "json", 1))                                                                       
      new = append(new, field)                                                                                                                                          
   }                                                                                                                                                                    
                                                                                                                                                                        
   newType := reflect.StructOf(new) //construct new type from fields which from foo struct                                                                              
   v := reflect.ValueOf(obj1)                                                                                                                                           
   v3 := v.Convert(newType)  //convert value to new type                                                                                                                
                                                                                                                                                                        
   fmt.Println(newType.Name()) // it's empty ""                                                                                                                         
   fmt.Println(st.Name()) // it's "foo"                                                                                                                                 
                                                                                                                                                                        
   return                                                                                                                                                               
}                                                                                                                                                                       
     

The code as above. There is chance to set “newType”.Name, and newType.Name() always return “”.
The other thing is “newType” from StructOf only contains the fields from “foo”, but no interface method Bar().

The name of such a type is always empty. See the documentation of reflect.Type:

// Name returns the type's name within its package for a defined type.
// For other (non-defined) types it returns the empty string.
Name() string

Where a defined type is a type defined using the

type Foo ...

syntax. A type created by reflection is not a defined type.

Thanks.
One more question: is it possible to add method to the type created by reflection? I want the new type “inheriting” all of fields and methods of “defined” type.

@Silajoin_Zhang that’s explicitly not supported, according to the documentation:

StructOf currently does not generate wrapper methods for embedded fields and panics if passed unexported StructFields. These limitations may be lifted in a future version.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.