Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

go - cannot use function (type func()) as type in argument

package main

import (
    "log"
    "strings"

    "asl.com/asl"
)

/*
Trivial service to demonstrate chaining service together
Message starts in originator, travels through a couple formatters, and then gets back to originator
*/

type MessageTest struct {
    Body string `json:"body"`
}
var s *asl.Service
func main() {
    var (
        err error
        cid string
    )
    //var m MessageDelivery
       var g asl.MessageHandler
       g = UpperCaseHandler

    //  UpperCaser := asl.NewService("UpperCaser", "", false)
    UpperCaser := asl.NewService("UpperCaser")
    if err = UpperCaser.ConsumeFunc("asl-service-uc", []string{"asl-service-uc"},func() interface{} {
        return ""
    },g); err != nil {
        log.Fatalf("Error starting consumer: %v", err)
    }
    //  Repeater := asl.NewService("Repeater", "", false)
    Repeater := asl.NewService("Repeater")
    if err = Repeater.ConsumeFunc("asl-service-repeat", []string{"asl-service-repeat"}, func() interface{} {
        return ""
    }, RepeatHandler); err != nil {
        //if err = Repeater.ConsumeFunc("asl-service-repeat", []string{"asl-service-repeat"}, mg asl.MessageGenerator, mh asl.MessageHandler); err != nil {
        log.Fatalf("Error starting consumer: %v", err)
    }

    //  originator := asl.NewService("Originator", "", false)
    originator := asl.NewService("Originator")

    deliveryChan := make(chan asl.MessageDelivery)
    m := asl.MessagePublishing{
        Message:     MessageTest{"this is a test"},
        RoutingKeys: []string{"asl-service-uc", "asl-service-repeat"},
    }
    if cid, err = originator.RPCPublish(m, deliveryChan); err != nil {
        log.Fatalf("Failed to publish: %v", err)
    }

    message := <-deliveryChan
    log.Printf("Originator Got: %+v", message.Message)
    originator.RemoveQueue(cid)
    UpperCaser.Wait()
}

func UpperCaseHandler(md asl.MessageDelivery) {
     s.Reply(MessageTest{strings.ToUpper(md.Message.(string))}, md.Delivery)
}

func RepeatHandler(md asl.MessageDelivery) {
     s.Reply(MessageTest{strings.Repeat(md.Message.(string), 5)}, md.Delivery)
}


package asl
Error 

./chains.go:26:10: cannot use UpperCaseHandler (typefunc(asl.MessageDelivery)) as type asl.MessageHandler in assignment ./chains.go:37:86: cannot use RepeatHandler (type func(asl.MessageDelivery)) as type asl.MessageHandler in argument to Repeater.ConsumeFunc

type MessageDelivery struct {
    Delivery amqp.Delivery
    Message  interface{}
    Error    error
    Context  *Context
}    
type MessageGenerator func() interface{}

type MessageHandler func(MessageDelivery) (interface{}, error)

I tried running the code,where am i going wrong,how do i right pass function as argument to another function. The function returns interface and error. Though function is taking MessageDelivery Struct as argument,function signature is same.where am i going wrong

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...