Golang, string type variable

Is it possible to create a variable with a type from a string?

Example:
  I have two types:

type FirstType struct {
    ...
}

type SecondType struct {
    ...
}

// also I have a string variable
var1 := "Second"

      

I want to create a variable with type - String value + "Type"

:

var variable = []var1+"Type" // slice of "SecondType"

      

The expected result is as in this case:

var variable = []SecondType

      

Thank!

+3


source to share


1 answer


It's impossible. Go does not provide functionality for creating variable types that are not statically known. The type of a variable is always known statically. Consider using interfaces instead.



+6


source







All Articles