From ec6a2fbfbc8def515939a97dc62134d05225a5dc Mon Sep 17 00:00:00 2001 From: mutil2 <146696499+mutil2@users.noreply.github.com> Date: Thu, 28 Dec 2023 10:02:44 +0300 Subject: [PATCH] Lab5.go --- golang/Lab5/Lab5.go | 48 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 golang/Lab5/Lab5.go diff --git a/golang/Lab5/Lab5.go b/golang/Lab5/Lab5.go new file mode 100644 index 00000000..99c4eff0 --- /dev/null +++ b/golang/Lab5/Lab5.go @@ -0,0 +1,48 @@ +package main + +import ( + "errors" +) + +type Dog struct { + Name string + Age int + Weight float64 +} + +func NewDog(dog_name string, dog_age int, dog_weight float64) (Dog, error) { + var d Dog = Dog{Name: dog_name} + err := d.SetAge(dog_age) + if err != nil { + return Dog{}, err + } + err = d.SetWeight(dog_weight) + if err != nil { + return Dog{}, err + } + return d, nil +} + +func (d *Dog) SetAge(age int) error { + if age < 0 { + return errors.New("Возраст не может быть отрицательным") + } + d.Age = age + return nil +} + +func (d *Dog) SetWeight(weight float64) error { + if weight <= 0 { + return errors.New("Вес не может быть меньше или равен 0") + } + d.Weight = weight + return nil +} + +func (d Dog) GetAge() int { + return d.Age +} + +func (d Dog) GetWeight() float64 { + return d.Weight +}