On 02/08/2017 10:55 PM, Ron Evans wrote:
|
packagemain

import"fmt"

type tableServer struct{
    table int
}

type TableServerinterface{
SetTable(table int)
GetTable()int
}

func NewTableServer()TableServer{
return&tableServer{}
}

func (i *tableServer)SetTable(table int){
    i.table =table
}

func (i *tableServer)GetTable()int{
returni.table
}

func SetTable(table int)func(*tableServer){
returnfunc(i *tableServer){
        i.SetTable(table)
}
}

type Waiterstruct{
Namestring
TableServer
}

func NewWaiter(name string,options ...func(*Waiter))*Waiter{
    w :=&Waiter{
Name: name,
TableServer:NewTableServer(),
}

for_,option :=range options {
        option(w)
}

returnw
}

func main(){
    waiter :=NewWaiter("joe")
    fmt.Println(waiter.Name,"is working table",waiter.GetTable())

// the following line cannot compile, with error:
// ./opts.go:56: cannot use SetTable(9) (type func(*tableServer)) as type func(*Waiter) in argument to NewWaiter
    waiter =NewWaiter("bob",SetTable(9))
    fmt.Println(waiter.Name,"is working table",waiter.GetTable())
}
|
Error is selfexplanatory, signatures func(*tableServer) and func(*Waiter) denote different types. Don't sure I understand precisely what you want to achieve, but changing
   func NewWaiter(name string, options ...func(TableServer)) *Waiter {
    ...
    //and
    func SetTable(table int) func(TableServer) {
        return func(i TableServer) {
            i.SetTable(table)
        }
    }
make code at least compile and run.
__
Ilya

--
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