Created
February 16, 2023 10:28
-
-
Save rohanjai777/00f3ed80d207ef432a467b00c51dbe17 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 IGun interface { | |
setName(name string) | |
setPower(power int) | |
getName() string | |
getPower() int | |
} | |
type Gun struct { | |
name string | |
power int | |
} | |
func (g *Gun) setName(name string) { | |
g.name = name | |
} | |
func (g *Gun) getName() string { | |
return g.name | |
} | |
func (g *Gun) setPower(power int) { | |
g.power = power | |
} | |
func (g *Gun) getPower() int { | |
return g.power | |
} | |
type Ak47 struct { | |
Gun | |
} | |
func newAk47() IGun { | |
return &Ak47{ | |
Gun: Gun{ | |
name: "AK47 gun", | |
power: 4, | |
}, | |
} | |
} | |
type musket struct { | |
Gun | |
} | |
func newMusket() IGun { | |
return &musket{ | |
Gun: Gun{ | |
name: "Musket gun", | |
power: 1, | |
}, | |
} | |
} | |
func getGun(gunType string) (IGun, error) { | |
if gunType == "ak47" { | |
return newAk47(), nil | |
} | |
if gunType == "musket" { | |
return newMusket(), nil | |
} | |
return nil, fmt.Errorf("Wrong gun type passed") | |
} | |
func main() { | |
ak47, _ := getGun("ak47") | |
musket, _ := getGun("musket") | |
printDetails(ak47) | |
printDetails(musket) | |
} | |
func printDetails(g IGun) { | |
fmt.Printf("Gun: %s", g.getName()) | |
fmt.Println() | |
fmt.Printf("Power: %d", g.getPower()) | |
fmt.Println() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment