Golang call another class

I’ve a method that takes text as a parameter. I wanna find the class from the incoming parameter and run a method. How can I do this with Reflect
I can access methods in same class in the image I took. I wanna access different class

Hi @mduzoylum, welcome to the forum.

If the parameter can take only a fixed number of known types, you can use a type switch:

switch v := i.(type) {
case T:
    // here v has type T
case S:
    // here v has type S
    // no match; here v has the same type as i
1 Like

Thank you for your answer.
I think this picture will explain better what I want.

You want to build a universal version of function getType, don’t you? I think it is impossible using reflection.

If @jauhararifin’s guess is correct that you want to look up any type by its name, you cannot do that with the reflect package. Like the Go Blog’s Laws of Reflection says:

  • Reflection goes from interface value to reflection object.
  • Reflection goes from reflection object to interface value.
  • To modify a reflection object, the value must be settable.

(emphasis mine). In other words, you need to already have a value of a type in order to use reflection on it.

Additionally, it seems you’re actually trying to look up a function from a package. This also cannot be done because reflection can only inspect members (methods, fields, array/slice elements, etc.) of values and cannot access global functions.

All that being said, I had a project where I wanted to do this, so I created github.com/skillian/pkgsyms. I put together this example from your screenshot:

package main

import (

	_ "forum.golangbridge.org/golang-call-another-class_25952/pts"
	_ "forum.golangbridge.org/golang-call-another-class_25952/yurtici"

// You need to add these go:generate lines for every package whose types you want
// to access.  That also means you have to run `go generate` before every build.
// Alternatively, you could put these go:generate lines inside of the packages
// themselves.

//go:generate pkgsyms -package main -output ptssyms.go -varname ptsPkg forum.golangbridge.org/golang-call-another-class_25952/pts
//go:generate pkgsyms -package main -output yurticisyms.go -varname yurticiPkg forum.golangbridge.org/golang-call-another-class_25952/yurtici

func main() {
	pkg := pkgsyms.Of("forum.golangbridge.org/golang-call-another-class_25952/pts")
	v, err := pkg.Lookup("Test")
	if err != nil {
	testFunc := v.Get().(func())

I found the solution, the solution is provided by accessing the structure within the structure I want to use

package main

import (
	ioc "go-kargo-consume/ioc"
	str "go-kargo-consume/structer"

func main(){

func setMethod(mtd string){
	md := str.Response{Isim: "Mehmet"}
	t := GetCargoType(mtd)

func GetCargoType(company string) reflect.Type {
	switch company {
	case "pts":
		return reflect.TypeOf(pts.PtsStruct{})
	case "yurtici":
		return reflect.TypeOf(yurtici.YurticiStruct{})
		return nil

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