Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package person
- type Name string
- type Age int
- type Person struct {
- name Name
- age Age
- }
- func NewPerson(name Name, age Age) *Person {
- return &Person{
- name: name,
- age: age,
- }
- }
- func GetName(p *Person) Name {
- return p.name
- }
- func SetName(p *Person, name Name) *Person {
- return &Person{
- name: name,
- age: p.age,
- }
- }
- func GetAge(p *Person) Age {
- return p.age
- }
- func SetAge(p *Person, age Age) (*Person, error) {
- if age < 0 {
- return nil, fmt.Errorf("age cannot be negative: %d", age)
- }
- return &Person{
- name: p.name,
- age: age,
- }, nil
- }
- //___________________________________________________________________________________________________
- package main
- import (
- "fmt"
- "person"
- )
- func main() {
- p := person.NewPerson("Alice", 30)
- // Get the person's name using the GetName function
- name := person.GetName(p)
- fmt.Printf("Name: %s\n", name)
- // Set the person's name using the SetName function
- p = person.SetName(p, "Bob")
- name = person.GetName(p)
- fmt.Printf("Name: %s\n", name)
- // Get the person's age using the GetAge function
- age := person.GetAge(p)
- fmt.Printf("Age: %d\n", age)
- // Set the person's age using the SetAge function
- var err error
- p, err = person.SetAge(p, 40)
- if err != nil {
- panic(err)
- }
- age = person.GetAge(p)
- fmt.Printf("Age: %d\n", age)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement