本文介绍了不能使用函数(类型 func())作为参数类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

包主进口 (日志"字符串"asl.com/asl")/*将服务链接在一起的琐碎服务消息从发起者开始,经过几个格式化程序,然后返回到发起者*/类型消息测试结构{正文字符串`json:"body"`}var s *asl.Service功能主(){变量 (错误错误字符串)//var m MessageDeliveryvar g asl.MessageHandlerg = 大写处理程序//UpperCaser := asl.NewService("UpperCaser", "", false)UpperCaser := asl.NewService("UpperCaser")if err = UpperCaser.ConsumeFunc("asl-service-uc", []string{"asl-service-uc"},func() interface{} {返回 ""},G);错误!= 零{log.Fatalf("启动消费者时出错:%v", err)}//中继器 := asl.NewService("中继器", "", false)中继器 := asl.NewService("中继器")if err = Repeater.ConsumeFunc("asl-service-repeat", []string{"asl-service-repeat"}, func() interface{} {返回 ""}, RepeatHandler);错误!= 零{//if err = Repeater.ConsumeFunc("asl-service-repeat", []string{"asl-service-repeat"}, mg asl.MessageGenerator, mh asl.MessageHandler);错误!= 零{log.Fatalf("启动消费者时出错:%v", err)}//originator := asl.NewService("Originator", "", false)发起人 := asl.NewService("发起人")DeliveryChan := make(chan asl.MessageDelivery)m := asl.MessagePublishing{消息:MessageTest{这是一个测试"},RoutingKeys: []string{"asl-service-uc", "asl-service-repeat"},}如果 cid, err = originator.RPCPublish(m, deliveryChan);错误!= 零{log.Fatalf("发布失败:%v", err)}消息 := <-deliveryChanlog.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)}包asl
错误

./chains.go:26:10: 不能使用 UpperCaseHandler (typefunc(asl.MessageDelivery)) 作为类型 asl.MessageHandler任务./chains.go:37:86: 不能使用 RepeatHandler (type func(asl.MessageDelivery)) 作为类型 asl.MessageHandlerRepeater.ConsumeFunc 的参数

type MessageDelivery struct {交付 amqp.Delivery消息接口{}错误错误上下文 *上下文}类型 MessageGenerator func() 接口{}类型 MessageHandler func(MessageDelivery)(接口{},错误)

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

解决方案

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

func UpperCaseHandler(md busboy.MessageDelivery) (interface{}, error} {s.Reply(MessageTest{strings.ToUpper(md.Message.(string))}, md.Delivery)归零,零}func RepeatHandler(md busboy.MessageDelivery) (interface{}, error} {s.Reply(MessageTest{strings.Repeat(md.Message.(string), 5)}, md.Delivery)归零,零}
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
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 returnsinterface and error. Though function is taking MessageDelivery Struct as argument,function signature is same.where am i going wrong

解决方案

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
}

这篇关于不能使用函数(类型 func())作为参数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 18:10