Create a factory of structs with reflect

i have problems implementing reflect for extract a type struct from a string.

Can you show what you are trying to achieve ? You can also check the code here https://github.com/golang/go/issues/460 which shows a known issue.

I have this…

i have a struct generic from the name, but show me the next error:

unsupported destination, should be slice or struct.

the project has gorm as orm.

please help

Hey @JUAN_JOSE_ORJUELA_CA,

Instead of posting an image, it will be much easier for people to help you if you can please post the code you are asking for help with at https://play.golang.org/, or at least post the code snippet indented with 4 spaces here for display like the following:

// Code goes here...

Kind regards!

my problem is I’m trying to make a gin-api rest in gonic with 15 tables.

Create the model layer, the layer repository and the web layer.

But I want to simplify the layer to a single generic repository file.

The idea initially was through a chain Reflect and draw a structura the selected model and make operations corresponding database, for this example is being used gorm.

But in doing so the structures are as interface {} and when passing gorm operations generates the following message:

unsupported destination, slice or struct Should be.

I want a suggestion or help to simplify the web layers and repository.

The repository is in the following url, I do not copy code because the project is large.

Your idea should work but you need to extract the type using .(type) and a pointer.
You can test your code using print(*T.(type)) for instance.
I pointed you to a known issue as you may try a switch type which need to be used very strictly.

I don’t fully understand your use case yet, but it might be worth looking at doing this without reflection. Generally, using Go’s native type handling is easier for others to read than using reflection. You could consider something like

func GetStructByName(name string) interface{} {
    switch t {
       case "s1":
          return S1{}
       case "s2":
          return S2{}
        //and so on
     }
}

live example: https://play.golang.org/p/V2SlO0H0Hh

so I have the code. but gorm not recognize the structure as S2 in your example and I generates the error specified above.

there any way to make a function where the return is not interface {}, but it dynamic? I mean according to the selected case returns me that structure and not an interface {}

In Go, using interfaces is the only way to return multiple types in the “dynamic” way you’re asking for. A function that returns interface{} will assert any returned value to interface{}, but you can use a type switch to get the original type back out. This tutorial can explain how that works better than I can :slight_smile:

Hey @JUAN_JOSE_ORJUELA_CA, are you looking for something like this?

package main

import (
	"fmt"
	"reflect"
)

type Object1 struct{}
type Object2 struct{}
type Object3 struct{}

var typeRegistry = map[string]reflect.Type{
	"one":   reflect.TypeOf(Object1{}),
	"two":   reflect.TypeOf(Object2{}),
	"three": reflect.TypeOf(Object3{}),
}

func GetStructByName(name string) interface{} {
	return reflect.ValueOf(typeRegistry[name]).Interface()
}

func main() {
	obj1 := GetStructByName("one")
	fmt.Println(obj1)

	obj2 := GetStructByName("two")
	fmt.Println(obj2)

	obj3 := GetStructByName("three")
	fmt.Println(obj3)
}
1 Like

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