5

How to have a variable whose type is a variable type?

What do I mean by that? I have a python and java background. In both languages I can do things like assigning a class name to variable.

#Python
class A:
    def __init__(self):
        self.a = "Some Value"

# And asigning the class name to a variable
class_A = A

# And create instance from class A by using class_A
a = class_A()

Is there such a mechanism in go that allows me to do that? I couldn't know where to look at for such things in their documentation. Honestly, generally I don't know what these are called in programming languages.

For example I would like to be able to do:

// For example, what would be the type of myType?
var myType type = int

I will use this mechanism to take "type" arguments. For example:

func Infer(value interface{}, type gotype) {...}

4 Answers 4

4

Is there such a mechanism in go that allows me to do that?

The short answer is: No.

The long answer is: This sounds like an XY Problem, so depending on what you're actually trying to accomplish, there may be a way. Jump to the end to see where I address your specific use-case.

I suspect that if you want to store a data type in a variable, it's because you either want to compare some other variable's type against it, or perhaps you want to create a variable of this otherwise unknown type. These two scenarios can be accomplished in Go.

In the first case, just use a type switch:

switch someVar.(type) {
    case string:
        // Do stringy things
    case int:
        // Do inty things
}

Semantically, this looks a lot like assigning the type of someVar to the implicit switch comparison variable, then comparing against various types.

In the second case I mentioned, if you want to create a variable of an unknown type, this can be done in a round-about way using reflection:

type := reflect.TypeOf(someVar)
newVar := reflect.New(type).Interface()

Here newVar is now an interface{} that wraps a variable of the same type as someVar.

In your specific example:

I will use this mechanism to take "type" arguments. For example:

func Infer(value interface{}, type gotype) {...}

No, there's no way to do this. And it actually has much less to do with Go's variable support than it does with the fact that Go is compiled.

Variables are entirely a runtime concept. Function signatures (like all other types in Go) are fixed at compilation time. It's therefore impossible for runtime variables to affect the compilation stage. This is true in any compiled language, not a special feature (or lack thereof) in Go.

Sign up to request clarification or add additional context in comments.

1 Comment

I see what your saying but I do think this could be supported in Go if they really wanted to. In F# for instance (a compiled language) it uses Discriminated Unions: type MyType = Person|Place|Thing type Something struct = { Name string MyObj MyType } While it may be syntactic sugar in F# it is extremely useful. Maybe I am misunderstanding?
4

Is there such a mechanism in go that allows me to do that?

No there is not.

Comments

-1

Use the empty interface:

var x, y interface{}
var a uint32
a = 255
x = int8(a)
y = uint8(a)

Playground example

1 Comment

This doesn't answer the question. The question asked how to assign the type itself to a variable, while the answer is assigning a variable to another variable.
-3

You mean like this?

var variableOfTypeByteArray []byte

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.