I am trying to understand Go's interface concept and create following code:
package main
import "fmt"
type Failer interface {
Error() string
}
type Succer interface {
Success() string
}
type Result interface {
Failer
Succer
}
type Fail struct{}
func (*Fail) Error() string {
return "Error"
}
type Succ struct{}
func (*Succ) Success() string {
return "Success"
}
type Combi struct{}
func (*Combi) Error() string {
return "Error"
}
func (*Combi) Success() string {
return "Success"
}
func main() {
var r Result
var s Succer
c := &Combi{}
r = c
s = c
fmt.Println(r.Error())
fmt.Println(s)
}
As output I've got
Error
Error
Why? I've expect as output error and success, because s
it is the interface of type Succer, there is no error return as string.
And when I change the main function like this:
func main() {
var r Result
var s Succer
c := &Combi{}
r = c
s = c
}
the compiler complain
# command-line-arguments
.\sample1.go:42: r declared and not used
.\sample1.go:43: s declared and not used
Why? I assign variables r
and s
a reference.