Created
December 15, 2024 19:55
-
-
Save JcsnP/c8e27f82c719f1d165c53474a18e9f8f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import "fmt" | |
| type Printer interface { | |
| Print() | |
| GetInformation() string | |
| } | |
| // ------------------------------------------------------------------ | |
| type SamsungPrinter struct { | |
| Name string | |
| IPAddress string | |
| } | |
| func (p *SamsungPrinter) Print() { | |
| fmt.Println("Printer is printing...") | |
| } | |
| func (p *SamsungPrinter) GetInformation() string { | |
| return fmt.Sprintf("Name: %s, IP Address: %s", p.Name, p.IPAddress) | |
| } | |
| func NewSamsungPrinter(name string, ipAddress string) Printer { | |
| return &SamsungPrinter{Name: name, IPAddress: ipAddress} | |
| } | |
| // ------------------------------------------------------------------ | |
| type EpsonPrinter struct { | |
| Name string | |
| IPAddress string | |
| } | |
| func (p *EpsonPrinter) Print() { | |
| fmt.Println("Epson Printer is printing...") | |
| } | |
| func (p *EpsonPrinter) GetInformation() string { | |
| return fmt.Sprintf("Name: %s, IP Address: %s", p.Name, p.IPAddress) | |
| } | |
| func NewEpsonPrinter(name string, ipAddress string) Printer { | |
| return &EpsonPrinter{Name: name, IPAddress: ipAddress} | |
| } | |
| // ------------------------------------------------------------------ | |
| func PrinterInfo(p Printer) { | |
| fmt.Println(p.GetInformation()) | |
| } | |
| func PrintersInfo(printers []Printer) { | |
| for _, printer := range printers { | |
| PrinterInfo(printer) | |
| } | |
| } | |
| func main() { | |
| var printer1 = NewSamsungPrinter("Samsung", "244.178.44.111") | |
| printer1.Print() | |
| var printer2 = NewEpsonPrinter("Epson", "244.178.44.111") | |
| printer2.Print() | |
| var printers = []Printer{printer1, printer2} | |
| PrintersInfo(printers) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment