Go Survik 'oop'

Run Settings
LanguageGo
Language Version
Run Command
package main import "fmt" var totalItemsCreated int = 0 type Item struct { name string description string useTime int useCount int } func NewItem(name, description string, useTime int) *Item { totalItemsCreated += 1 return &Item{name: name, description: description, useTime: useTime, useCount: 0} } func (i *Item) Use() string { i.useCount += 1 return fmt.Sprintf("Používá se %s (%s), což trvá %d sekund. Počet použití: %d.", i.name, i.description, i.useTime, i.useCount) } func GetTotalItemsCreated() int { return totalItemsCreated } type Survik struct { name string item *Item } func NewSurvik(name string, item *Item) *Survik { return &Survik{name: name, item: item} } func (s *Survik) SetItem(item *Item) { s.item = item } func (s *Survik) UseItem() { itemLine := s.item.Use() fmt.Printf("%s používá %s: %s\n", s.name, s.item.name, itemLine) } func main() { baterka := NewItem("Baterka", "osvětluje cestu", 0) fmt.Println(baterka.Use()) lekarnicka := NewItem("Lékárnička", "léčí zranění", 20) fmt.Println(lekarnicka.Use()) fmt.Println(GetTotalItemsCreated()) pepa := NewSurvik("Pepik the survik", baterka) pepa.UseItem() pepa.SetItem(lekarnicka) pepa.UseItem() }
Editor Settings
Theme
Key bindings
Full width
Lines