不能使用函数(类型func())作为Golang参数的类型

loading...


-2
package main

import (
    "log"
    "strings"

    "go.scope.charter.com/busboy"
)

/*
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 *busboy.Service
func main() {
    var (
        err error
        cid string
    )
    //var m MessageDelivery
       var g busboy.MessageHandler
       g = UpperCaseHandler

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

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

    deliveryChan := make(chan busboy.MessageDelivery)
    m := busboy.MessagePublishing{
        Message:     MessageTest{"this is a test"},
        RoutingKeys: []string{"rio-service-uc", "rio-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 busboy.MessageDelivery) {
     s.Reply(MessageTest{strings.ToUpper(md.Message.(string))}, md.Delivery)
}

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


package busboy

错误
./chains.go:26:10:不能使用UpperCaseHandler(typefunc(busboy.MessageDelivery))作为类型的busboy.MessageHandler赋值./chains.go:37:86:不能使用RepeatHandler(类型为func(busboy.MessageDelivery))作为类型busboy.MessageHandler参与Repeater.ConsumeFunc的参数

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

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

我尝试运行代码,我哪里出错了,我如何将函数作为参数传递给另一个函数 . 该函数返回接口和错误 . 虽然函数将MessageDelivery Struct作为参数,但函数签名是相同的 . 我在哪里出错了

1回答

  • 0

    您正确地将函数作为参数传递,但它们与预期的签名不匹配 . 将您的功能更改为:

    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
    }
    

loading...

评论

暂时没有评论!