亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在類型為map[int]的接口上實現接口

如何在類型為map[int]的接口上實現接口

Go
qq_花開花謝_0 2023-07-10 14:35:41
我正在嘗試構建一個可以在 map[int]T 上實現的函數。我是個新手,想知道這是否可以通過實現接口來完成。    invoices   map[int]domain.Invoice    bookings   map[int]domain.Booking    projects   map[int]domain.Project所有這些都有以下共同點:type Invoice struct {    ID         int}type Booking struct {    ID         int}type Project struct {    ID         int}我必須如何繼續實現一個函數,通過增加相應類型映射中最后一個項目的 ID 來返回所有發票、預訂或項目的下一個 ID?例如:func (i *map[int]T) nextID() int {    return T.ID + 1
查看完整描述

3 回答

?
慕姐8265434

TA貢獻1813條經驗 獲得超2個贊

收集到所有信息后,我得出以下答案:

https://play.golang.org/p/3RAvclRacbh

(為了方便命名,我使用了“List”)

package main


import (

    "fmt"

)


type Invoice struct{

    Name string

    Id int

}


// Interface to implement

type IDer interface {

    ID() int

}

// Interface implementation for Invoice

func (i Invoice) ID() int { return i.Id }


type List struct {

    Items    map[int]IDer

    last IDer

}


func (i *List) Add(index int, ider IDer) {

    i.Items[index] = ider

    i.last = ider

}


func (i *List) nextID() int {

        if i.last == nil {

          return 1

        }

    return i.last.ID() + 1

}


type Repository struct {

    invoices List

}


func main() {

    r := Repository{}

    r.invoices = List{ 

        Items: make(map[int]IDer), 

    }


    i := Invoice{}

    i.Name = "Test"

    i.Id = 1

    r.invoices.Add(1, i)

    ia := r.invoices.Items[1].(Invoice) 

    fmt.Println(ia.Name)

    fmt.Printf("Next ID: %d\n", r.invoices.nextID())



    i2 := Invoice{}

    i2.Name = "Test2"

    i2.Id = r.invoices.nextID()

    r.invoices.Add(i2.Id, i2)

    ia2 := r.invoices.Items[i2.Id].(Invoice)    

    fmt.Println(ia2.Name)

    fmt.Printf("Next ID: %d\n", r.invoices.nextID())



    i3 := Invoice{}

    i3.Name = "Test3"

    i3.Id = r.invoices.nextID()

    r.invoices.Add(i3.Id, i3)

    ia3 := r.invoices.Items[i3.Id].(Invoice)    

    fmt.Println(ia3.Name)

    fmt.Printf("Next ID: %d\n", r.invoices.nextID())

}


Test

Next ID: 2

Test2

Next ID: 3

Test3

Next ID: 4


Program exited.


查看完整回答
反對 回復 2023-07-10
?
藍山帝景

TA貢獻1843條經驗 獲得超7個贊

wondered, if this can be done by implementing an interface當然可以。

我不會假裝提供正確的慣用方式。但從你的說法來看,所有這些類型都可以歸納為一個IDer接口。

為此,他們必須實現一個定義為 的接口type IDer interface { ID() int }。ID()必須為每個結構類型實現此方法。使用通用的兼容實現,您可以定義 a并將map[int]IDer其放入其中Invoice,只要它兼容即可。BookingProject

為了防止代碼重復,您可以定義一個type IDed struct { ID int}; func (i ID) int {return i.ID},并將其嵌入到您的類型中,例如type Invoice struct { IDed }等等。這樣做您仍然可以打電話var x Invoice;  x.ID=1; someid := x.ID;

最后,您可以將映射定義為類型type mapOfID map[int]IDer并向其附加方法func (m mapOfID) DoSomethigWithIDers(){...}


查看完整回答
反對 回復 2023-07-10
?
炎炎設計

TA貢獻1808條經驗 獲得超4個贊

地圖本身在跟蹤“最后”項目方面做得很差,因為它不是其存儲或語義的一部分。解決這個問題的一種方法是跟蹤最后一個索引或domain添加到映射中的最后一個對象,以便計算下一個 id:


type IDer interface {

    ID() int

}

type IDS struct {

    m    map[int]IDer

    last IDer

}


func (i *IDS) Add(index int, ider IDer) {

    i.m[index] = ider

    i.last = ider

}


func (i *IDS) nextID() int {

    if i.last == nil {

      return 1

    }

    return i.last.ID() + 1

}

上面的示例結合了 @mh-cbon 的 IDer 接口和跟蹤最后添加的 Ider 的能力。


domain然后,只要任何對象實現了該接口,就可以將其與它們一起使用IDer:


type Invoice struct{}


func (i Invoice) ID() int { return 1 }


func main() {

    ids := &IDS{

       m: make(map[int]IDer),

    }

    ids.Add(1, Invoice{})

    fmt.Printf("Next ID: %d\n", ids.nextID())


}

Next ID: 2


Program exited.


查看完整回答
反對 回復 2023-07-10
  • 3 回答
  • 0 關注
  • 202 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號