MessageImpl不实现Message

时间:2015-07-24 16:01:01

标签: go

在golang中,我遇到以下编译错误:

    SELECT qryReportWSLCurrentQty.Location,
      qryReportWSLCurrentQty.WarehouseName,
      tblStock.SupplierRef,
      qryReportWSLCurrentQty.StockCode,
      SUM(qryReportWSLCurrentQty.SumOfMovementQty)      AS WarehouseQty,
      SUM(qryReportWSLCurrentQty.SumOfMovementQtyBoxes) AS WarehouseBoxes,
      qryDeliveryValue.LastOfMovementDate               AS DelDate,
      [qryCostValueForReports].[CostValue],
      IIf(([qryTotalSalesValue].[NetAmount]IS NULL),0,[qryTotalSalesValue].[NetAmount])   AS [SalesValue],
   IIf(IIf(([qryTotalSalesValue].[NetAmount]IS NULL),0,[qryTotalSalesValue].[NetAmount])                      =0,-100,((IIf(([qryTotalSalesValue].[NetAmount]IS NULL),0,[qryTotalSalesValue].[NetAmount]) /[qryCostValueForReports].[CostValue])*100)-100) AS PerCentUp,
   IIf(([SumOfQty]                      IS NULL),0,[SumOfQty])                         AS ShopStock
    FROM ((((qryReportWSLCurrentQty
    INNER JOIN tblStock
      ON qryReportWSLCurrentQty.StockCode = tblStock.StockCode)
    LEFT JOIN QRYDELIVERYVALUE
      ON QRYREPORTWSLCURRENTQTY.STOCKCODE = QRYDELIVERYVALUE.STOCKCODE)
    LEFT JOIN qryTotalStockinShops
      ON qryReportWSLCurrentQty.StockCode = qryTotalStockinShops.StockCode)
    LEFT JOIN QRYTOTALSALESVALUE
      ON qryReportWSLCurrentQty.StockCode = qryTotalSalesValue.StockCode)
    LEFT JOIN QRYCOSTVALUEFORREPORTS
      ON qryReportWSLCurrentQty.StockCode = qryCostValueForReports.StockCode
    WHERE (((tblStock.DeadCode)         =0))
    GROUP BY QRYREPORTWSLCURRENTQTY.LOCATION,
      QRYREPORTWSLCURRENTQTY.WAREHOUSENAME,
      TBLSTOCK.SUPPLIERREF,
      QRYREPORTWSLCURRENTQTY.STOCKCODE,
      QRYDELIVERYVALUE.LASTOFMOVEMENTDATE,
      QRYCOSTVALUEFORREPORTS.COSTVALUE,
      IIF(([QRYTOTALSALESVALUE].[NETAMOUNT]    IS NULL ),0,[QRYTOTALSALESVALUE].[NETAMOUNT]),
      IIF(([SUMOFQTY]                          IS NULL),0,[SUMOFQTY])
    HAVING (((qryCostValueForReports.CostValue)<>0));

使用以下代码:

cannot use m (type MessageImpl) as type Message in assignment:
    MessageImpl does not implement Message (missing FirstLine method)

来自java语言,我会尝试表达我的意图: 有一些接口type Message interface { FirstLine() string /* some other method declarations*/ } type MessageImpl struct { headers []Header /* some other fields */ } type Request struct { MessageImpl /* some other fields */ } type Response struct { MessageImpl /* some other fields */ } func (r Request) FirstLine() string { var requestLine bytes.Buffer requestLine.WriteString("...") // Some more instructions return requestLine.String() } func (m MessageImpl) RenderMessage() string { var message bytes.Buffer var msg Message = m // <=== This is the line the compiler moans about message.WriteString(msg.FirstLine()) // Some more instructions return message.String() } 定义了抽象类Message实现的一些方法。两个真实的类MessageImplRequest应该从Response继承(并实现接口MessageImpl)。这两个都定义并实现了一些更内部的方法。

这怎么做得最好?

1 个答案:

答案 0 :(得分:2)

go中的接口是一组方法,但接口也是类型。因此,go约定是为任何接口添加后缀,以指示其他类型是否构成其中的方法。

您需要为Messege类型定义一个实现,以接收FirstLine()方法。它因此满足Messeger接口。

编辑:我误解了你在使用bytes.Buffer做了什么,所以答案实际上要简单得多。

Edit2:我不得不考虑一下这一点,但似乎你希望RenderMessage()成为一个函数而不是一个方法。 RenderMessage的参数类型为Messeger,可以(到目前为止)为RequestResponseMessage类型。

如果您传递的RenderMessage可以是变量类型,则无法为Messager定义方法接收器,除非您希望在RenderMessage内进行类型切换,在某种程度上违背了使用界面开始的目的。

package main

import (
    "bytes"
    "fmt"
)

type Messager interface {
    FirstLine() string
    /* some other method declarations*/
}

type Header struct {
    Data string
}

type Message struct {
    headers []Header
    /* some other fields */
}

type Request struct {
    /* some fields */
    Message
}

type Response struct {
    /* some fields */
    Message
}

func (r Request) FirstLine() string {
    var requestLine bytes.Buffer
    requestLine.WriteString("...")
    // Some more instructions
    return requestLine.String()
}

func (r Response) FirstLine() string {
    var requestLine bytes.Buffer
    requestLine.WriteString("!!!")
    // Some more instructions
    return requestLine.String()
}

func (m Message) FirstLine() string {
    // whatever this would be
    return "???"
}

func RenderMessage(m Messager) string {
    var b bytes.Buffer
    var msg Messager = m // <=== This is the line the compiler moans about
    b.Write([]byte("some text"))
    b.WriteString(msg.FirstLine())
    // Some more instructions
    return b.String()
}

func main() {
    header := Request{}
    fmt.Println(RenderMessage(header))

    resp := Response{}
    fmt.Println(RenderMessage(resp))

    foo := Message{}
    fmt.Println(RenderMessage(foo))
}

输出:

some text...
some text!!!
some text???

我添加了这个,因为它没有在您的问题中定义:

type Header struct {
    Data string
}

我还添加了这一行来说明这个例子:

    b.Write([]byte("some text"))

http://play.golang.org/p/n-DOQbXXl9