🪟 Windows TippsThe Gemini desktop app is now available for Windows(11.09.2026 um 17:06 Uhr)
🕵️ SicherheitslückenBurn Out, Or Fade Away(14.09.2026 um 14:25 Uhr)
⚠️ Malware / Trojaner / VirenWindows 11 just dropped the tool ransomware abused, Microsoft says don’t restore WMIC(10.09.2026 um 20:11 Uhr)
⚠️ Malware / Trojaner / VirenWindows 11: Microsoft entfernt WMIC-Tool gegen Ransomware - ad-hoc-news.de(14.09.2026 um 07:58 Uhr)
🕵️ SicherheitslückenMicrosoft schließt Rekordzahl an Sicherheitslücken - techbook(14.09.2026 um 09:00 Uhr)
🪟 Windows TippsThe Gemini desktop app is now available for Windows(11.09.2026 um 17:06 Uhr)
🕵️ SicherheitslückenBurn Out, Or Fade Away(14.09.2026 um 14:25 Uhr)
⚠️ Malware / Trojaner / VirenWindows 11 just dropped the tool ransomware abused, Microsoft says don’t restore WMIC(10.09.2026 um 20:11 Uhr)
⚠️ Malware / Trojaner / VirenWindows 11: Microsoft entfernt WMIC-Tool gegen Ransomware - ad-hoc-news.de(14.09.2026 um 07:58 Uhr)
🕵️ SicherheitslückenMicrosoft schließt Rekordzahl an Sicherheitslücken - techbook(14.09.2026 um 09:00 Uhr)

🔧 Programmierung 🕛 vor 6 Monaten 2 Min Lesezeit
0

Understanding Go Interfaces with a Practical Example

↗ Quelle (dev.to)
🗣️ Stimme:

One of the most powerful features in Go is interfaces. They allow different types to share behaviour without requiring inheritance or explicit declarations.



If you're coming from languages like Java or C#, Go interfaces can feel unusual at first because types don’t explicitly declare that they implement them.



Instead, if a type implements the required methods, it automatically satisfies the interface.



In the code below we'll walk through a small practical example showing how interfaces allow different implementations to behave differently while still sharing the same contract.




CODE
package main

import "fmt"

// Adjustable represents something whose balance can be increased or decreased.
type Adjustable interface {
Add(amount float64)
Subtract(amount float64)
GetBalance() float64
}

//
// STANDARD WALLET
//

type StandardWallet struct {
balance float64
}

// Constructor
func NewStandardWallet() *StandardWallet {
return &StandardWallet{}
}

func (w *StandardWallet) Add(amount float64) {
w.balance += amount
}

func (w *StandardWallet) Subtract(amount float64) {
w.balance -= amount
}

func (w *StandardWallet) GetBalance() float64 {
return w.balance
}

//
// WALLET WITH TRANSACTION FEE
//

type FeeWallet struct {
balance float64
fee float64
}

// Constructor
func NewFeeWallet(fee float64) *FeeWallet {
return &FeeWallet{fee: fee}
}

func (w *FeeWallet) Add(amount float64) {
w.balance += amount
}

// Subtract applies an additional fee.
func (w *FeeWallet) Subtract(amount float64) {
w.balance -= (amount + w.fee)
}

func (w *FeeWallet) GetBalance() float64 {
return w.balance
}

//
// GENERIC LOGIC
//

// AdjustBalance works with any type that satisfies Adjustable.
func AdjustBalance(a Adjustable, add bool, amount float64) {
if add {
a.Add(amount)
} else {
a.Subtract(amount)
}
}

func main() {

standard := NewStandardWallet()
feeWallet := NewFeeWallet(2)

// Same logic applied to both implementations
AdjustBalance(standard, true, 100)
AdjustBalance(standard, false, 30)

AdjustBalance(feeWallet, true, 100)
AdjustBalance(feeWallet, false, 30)

fmt.Println("Standard wallet balance:", standard.GetBalance())
fmt.Println("Fee wallet balance:", feeWallet.GetBalance())
}


Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
The Gemini desktop app is now available for Windows
1 Quelle
Burn Out, Or Fade Away
1 Quelle
Windows 11 just dropped the tool ransomware abused, Microsoft says don’t restore WMIC
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Understanding Go Interfaces with a Practical Example

Thematisch verwandte Begriffe: Understanding, Interfaces, with, Practical · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...