首頁 後端開發 Golang 如何使用Go中的介面實作不同類型的方法而不重複程式碼?

如何使用Go中的介面實作不同類型的方法而不重複程式碼?

Nov 15, 2024 am 06:35 AM

How Can Interfaces in Go Be Used to Implement Methods for Different Types Without Code Duplication?

Implementing Methods for Different Types Using Interfaces in Go

In Go, interfaces provide a mechanism to define a set of methods that various types can implement. This allows for polymorphic behavior, where different types can share a common set of operations.

Problem Statement

Suppose we have two structs, First and Second, that we want to make implement an interface, A, which defines a method called PrintStr(). Each struct has a string field named str. However, if we implement the PrintStr() method separately for both structs, it becomes redundant.

Proposed Solution

One might think it's possible to have a single implementation of PrintStr() for both structs by defining it on the A interface itself. However, this approach doesn't work because interfaces do not carry any data.

Embedding and Implementing

Instead of a single implementation on the interface, we can create a base type, WithString, and embed it into both First and Second. The WithString type will have the str field and implement the PrintStr() method. By embedding WithString, both First and Second indirectly gain the str field and the PrintStr() implementation. This allows them to implement the A interface with minimal duplication.

Example Code

type WithString struct {
    str string
}

type First struct {
    WithString
}

type Second struct {
    WithString
}

type A interface {
    PrintStr() //print First.str or Second.str
}

func (w WithString) PrintStr() {
    fmt.Print(w.str)
}
登入後複製

Usage

a := First{
    WithString: WithString{
        str: "foo",
    },
}
登入後複製

This approach allows us to implement a common method for different types while avoiding code duplication.

以上是如何使用Go中的介面實作不同類型的方法而不重複程式碼?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱門文章

倉庫:如何復興隊友
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章

倉庫:如何復興隊友
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章標籤

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Go語言包導入:帶下劃線和不帶下劃線的區別是什麼? Go語言包導入:帶下劃線和不帶下劃線的區別是什麼? Mar 03, 2025 pm 05:17 PM

Go語言包導入:帶下劃線和不帶下劃線的區別是什麼?

Beego框架中NewFlash()函數如何實現頁面間短暫信息傳遞? Beego框架中NewFlash()函數如何實現頁面間短暫信息傳遞? Mar 03, 2025 pm 05:22 PM

Beego框架中NewFlash()函數如何實現頁面間短暫信息傳遞?

如何編寫模擬對象和存根以進行測試? 如何編寫模擬對象和存根以進行測試? Mar 10, 2025 pm 05:38 PM

如何編寫模擬對象和存根以進行測試?

如何使用跟踪工具了解GO應用程序的執行流? 如何使用跟踪工具了解GO應用程序的執行流? Mar 10, 2025 pm 05:36 PM

如何使用跟踪工具了解GO應用程序的執行流?

如何定義GO中仿製藥的自定義類型約束? 如何定義GO中仿製藥的自定義類型約束? Mar 10, 2025 pm 03:20 PM

如何定義GO中仿製藥的自定義類型約束?

Go語言如何便捷地寫入文件? Go語言如何便捷地寫入文件? Mar 03, 2025 pm 05:15 PM

Go語言如何便捷地寫入文件?

Go語言中如何將MySQL查詢結果List轉換為自定義結構體切片? Go語言中如何將MySQL查詢結果List轉換為自定義結構體切片? Mar 03, 2025 pm 05:18 PM

Go語言中如何將MySQL查詢結果List轉換為自定義結構體切片?

如何編寫基準,以準確反映GO中的現實世界表現? 如何編寫基準,以準確反映GO中的現實世界表現? Mar 10, 2025 pm 05:36 PM

如何編寫基準,以準確反映GO中的現實世界表現?

See all articles