Is it possible to create variable with type from string?
Example:
I have two types:
type FirstType struct {
...
}
type SecondType struct {
...
}
// also I have a string variable
var1 := "Second"
I want to create variable with type - String value + "Type"
:
var variable = []var1+"Type" // slice of "SecondType"
Expected result is like in this case:
var variable = []SecondType
Thanks!
This is not possible. Go does not provide functionality to create variables of types that are not known statically. The type of a variable is always known statically. Consider using interfaces instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With