Golang中的实现接口

I want to implement the interface shown below. I don't know how to begin. Can someone show me how the functions should be implemented?

package interval
package main

type Interval interface {
    contains(r float64) bool // if r is in x, then true
    average(Y Intervall) (Intervall, error)
    String() string                    //cast interval"[a,b]" to [a,b]
    completecontains(Y Intervall) bool //if y is completely in x, give true
    New(a, b float64) Intervall
    //var a int
}
type Complex struct {
    first int
}

func (c Complex) contains(r float64) bool {
    if a <= r <= b {
        return true
    } else {
        return false
    }
}

func (c Complex) String() string {
    return "a"
}

func (c Complex) length() float64 {
    return 2.3
}

func main() {
}

I can't really tell what you are actually trying to do here, but there were several issues with the code

  1. a and b were not defined, I added them to complex to get it to compile
  2. a <= r <= b is not valid in go, changed that
  3. You had a main, so I assume that you meant this to be the runnable app. Package needs to be called "main" for it to be directly runnable.

May not be what you want, but it now compiles and runs (but doesn't do anything since main is empty)

Here it is on play

package main

//import "fmt"

type Intervall interface {
    contains(r float64) bool // if r is in x, then true
    average(Y Intervall) (Intervall, error)
    String() string                    //cast interval"[a,b]" to [a,b]
    completecontains(Y Intervall) bool //if y is completely in x, give true
    New(a, b float64) Intervall
}

type Complex struct {
    first int
    a     float64
    b     float64
}

func (c Complex) contains(r float64) bool {

    if c.a <= r && r <= c.b {
        return true
    } else {
        return false
    }
}

func (c Complex) String() string {
    return "a"

}
func (c Complex) length() float64 {
    return 2.3
}

func main() {

}

Not sure why the concrete interval is called "Complex" or what the average of two intervals might be, but this is as close as I can get. Also, not sure what the benefit of using an interface is here.

http://play.golang.org/p/sxFRkJZCFa

package main

import "fmt"

type Interval interface {
    Contains(r float64) bool
    Average(y Interval) (Interval, error)
    String() string
    CompletelyContains(y Interval) bool
    CompletelyContainedBy(y Interval) bool
}

type Complex struct {
    a, b float64
}

func (c Complex) Contains(r float64) bool {
    return c.a <= r && r <= c.b
}

func (c Complex) Average(y Interval) (Interval, error) {
    return nil, fmt.Errorf("What the heck is the average of two intervals?")
}

func (c Complex) CompletelyContains(y Interval) bool {
    return y.CompletelyContainedBy(c)
}

func (c Complex) CompletelyContainedBy(y Interval) bool {
    return y.Contains(c.a) && y.Contains(c.b)
}

func (c Complex) String() string {
    return fmt.Sprintf("[%v,%v]", c.a, c.b)
}

func main() {
    var x Interval = Complex{a: 1, b: 5.1}
    var y Interval = Complex{a: 1.3, b: 5}
    fmt.Println("x contains 3:", x.Contains(3))
    fmt.Println("x completely contains y:", x.CompletelyContains(y))

    avg, err := x.Average(y)
    fmt.Println("Average of x and y:", avg, "with error:", err)

    fmt.Println("x:", x)

}

Edit: Here's a sillily complex way of implementing "Average" the way you want it. The complexity comes from avoiding directly accessing y.a and y.b, which would defeat the purpose of using an interface (if there is one).

http://play.golang.org/p/Tc5YCciLWq