-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |