--- url: "https://refactoring.guru/design-patterns/bridge/go/example" titulek: "Bridge in Go / Design Patterns" h1: "Bridge in Go" popis: "Bridge pattern in Go. Full code example in Go with detailed comments and explanation. Bridge is a structural design pattern that divides business logic or huge class into separate class hierarchies that can be developed independently." jazyk: "en" kanonicka: "https://refactoring.guru/design-patterns/bridge/go/example" drobecky: ["Home", "Design Patterns", "Bridge", "Go"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-13 21:27:26" znaku: 3038 slov: 413 kodovani: "utf-8" sha256: "cfb233d03a7764c40e4c26c1a52fbb97a16d5ed3721425617087952f3626fb7a" --- # **Bridge** in Go **Bridge** is a structural design pattern that divides business logic or huge class into separate class hierarchies that can be developed independently. One of these hierarchies (often called the Abstraction) will get a reference to an object of the second hierarchy (Implementation). The abstraction will be able to delegate some (sometimes, most) of its calls to the implementations object. Since all implementations will have a common interface, they’d be interchangeable inside the abstraction. [Learn more about Bridge](/design-patterns/bridge) ## Conceptual Example Say, you have two types of computers: Mac and Windows. Also, two types of printers: Epson and HP. Both computers and printers need to work with each other in any combination. The client doesn’t want to worry about the details of connecting printers to computers. If we introduce new printers, we don’t want our code to grow exponentially. Instead of creating four structs for the 2*2 combination, we create two hierarchies: - Abstraction hierarchy: this will be our computers - Implementation hierarchy: this will be our printers These two hierarchies communicate with each other via a Bridge, where the Abstraction (computer) contains a reference to the Implementation (printer). Both the abstraction and implementation can be developed independently without affecting each other. #### **computer.go:** Abstraction ``` package main type Computer interface { Print() SetPrinter(Printer) } ``` #### **mac.go:** Refined abstraction ``` package main import "fmt" type Mac struct { printer Printer } func (m *Mac) Print() { fmt.Println("Print request for mac") m.printer.PrintFile() } func (m *Mac) SetPrinter(p Printer) { m.printer = p } ``` #### **windows.go:** Refined abstraction ``` package main import "fmt" type Windows struct { printer Printer } func (w *Windows) Print() { fmt.Println("Print request for windows") w.printer.PrintFile() } func (w *Windows) SetPrinter(p Printer) { w.printer = p } ``` #### **printer.go:** Implementation ``` package main type Printer interface { PrintFile() } ``` #### **epson.go:** Concrete implementation ``` package main import "fmt" type Epson struct { } func (p *Epson) PrintFile() { fmt.Println("Printing by a EPSON Printer") } ``` #### **hp.go:** Concrete implementation ``` package main import "fmt" type Hp struct { } func (p *Hp) PrintFile() { fmt.Println("Printing by a HP Printer") } ``` #### **main.go:** Client code ``` package main import "fmt" func main() { hpPrinter := &Hp{} epsonPrinter := &Epson{} macComputer := &Mac{} macComputer.SetPrinter(hpPrinter) macComputer.Print() fmt.Println() macComputer.SetPrinter(epsonPrinter) macComputer.Print() fmt.Println() winComputer := &Windows{} winComputer.SetPrinter(hpPrinter) winComputer.Print() fmt.Println() winComputer.SetPrinter(epsonPrinter) winComputer.Print() fmt.Println() } ``` #### **output.txt:** Execution result ``` Print request for mac Printing by a HP Printer Print request for mac Printing by a EPSON Printer Print request for windows Printing by a HP Printer Print request for windows ``` ## **Bridge** in Other Languages