Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional variable declaration in golang?

Tags:

go

Is it possible to do conditional variable type declaration like this in Golang?

if isAdmin {
  var result NormalResult
} else {
  var result AdminResult
}

// do something to &result
doSomething(&result)

func doSomething(interface{}) {
  // something
}

The above does not work, but the ideas is that normalResult and adminResults are very similar structs and how would I go about doing this?

Thank you!

like image 677
samol Avatar asked Oct 09 '13 18:10

samol


2 Answers

No, not in this manner. Go being statically typed, needs to know the type information at compile time.

What you could do is declare result as an interface of some type which both AdminResult and NormalResult satisfy. You can then use a type assertion at runtime to decide which type of result it is.

(You also have to declare result outside of the if blocks because Go is block scoped)

type NormalResult struct {
    Value int
}

func (r NormalResult) Result() int {
    return r.Value
}

type AdminResult struct {
    Value int
}

func (r AdminResult) Result() int {
    return r.Value
}

type Resulter interface {
    Result() int
}

func main() {
    isAdmin := true
    var r Resulter

    if isAdmin {
        r = AdminResult{2}
    } else {
        r = NormalResult{1}
    }

    fmt.Println("Hello, playground", r)

}
like image 91
JimB Avatar answered Nov 12 '22 22:11

JimB


Depending on what kind of similarities, you might have different options.

Using embedded structs

Depending on your structure, you might be able to use embedded structs. Let's say NormalResult is defined like this:

type NormalResult struct {
    Name  string
    Value int
}

And if AdminResult shares the same properties but just adds a few more of them (like UserId), you can choose to embed NormalResult into the AdminResult like this:

type AdminResult struct {
    *NormalResult
    UserId int64
}

Then you can also declare methods for NormalResult which will be promoted to AdminResult as well:

func (r *NormalResult) doSomething() {
    // Doing something
}

Edit
And, no, it is not possible to have conditional types in Go as you suggested. A variable can only be of one type, be it NormalResult, AdminResult or interface{}

like image 37
ANisus Avatar answered Nov 12 '22 22:11

ANisus