dsfs5420
dsfs5420
采纳率100%
2018-05-07 05:37

不能在参数中使用函数(func()类型)

  • interface
已采纳
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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dongwei7245 dongwei7245 3年前

    You are passing the function as an argument correctly but they do not match the expected signature. Change your functions to:

    func UpperCaseHandler(md busboy.MessageDelivery) (interface{}, error} {
         s.Reply(MessageTest{strings.ToUpper(md.Message.(string))}, md.Delivery)
         return nil, nil
    }
    
    func RepeatHandler(md busboy.MessageDelivery) (interface{}, error} {
         s.Reply(MessageTest{strings.Repeat(md.Message.(string), 5)}, md.Delivery)
         return nil, nil
    }
    
    点赞 评论 复制链接分享

为你推荐