I'm trying to better understand how to properly use interfaces and created 
this scenario:

Let's say I have an 'evaluator' program, that over time will want to add 
more and more evaluations of a Go-NoGo nature (Nasa?)

Here's want I was trying:

// My Go-NoGo interface - method should return true/false and an error is 
false
type GoNoGoer interface {
Eval() (bool, error)
}


Here's a couple of concrete evaluation types:
----------

type Evaluation1 struct {
Data string
}

func (a *Evaluation1) Eval() (bool, error) {
isOk := a.DoMyEvaluationWithTheData()
if !isOk {
return false, fmt.Errorf("evaluation failed because ...")
}
return true, nil
}

type Evaluation2 struct {
SomeData string
    MoreData int64
}

func (a *Evaluation2) Eval() (bool, error) {
isOk := a.DoMyEvaluationWithTheStructData()
if !isOk {
return false, fmt.Errorf("evaluation failed because ...")
}
return true, nil
}

----------

In main - iterate over all the evaluations and print the results of Eval() 

func main() {
    one := Evaluation1{Data: "abcabcabc"}
    two := Evaluation2{SomeData: "xyzxyz", MoreDate: 128}

    var evals []interface{}    <------ So I want a slice of evaluations 
(all implemented as various structs)  - probably not correct to use 
interface{}???
    evals = append(evals, one)
    evals = append(evals, two)

    // range over the slice, and if the object has implemented the Eval() 
method execute it and print the results  (doesn't work)
for _, eval := range evals {
if ok := eval.(GoNoGoer); ok {     <-- This also is wrong, I want to go the 
other direction (from concrete to interface)
isOk, err := eval.Eval()
fmt.Println(ok, err)
}
}
}


I'd appreciate the slap across the head pointing out where I'm missing the 
boat!

TIA

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to