trying_out_go/playground/magazineSubscribers.go

47 lines
952 B
Go
Raw Normal View History

2023-12-20 22:01:29 -05:00
// from chapter 8 of head first go
package main
2024-01-16 10:21:20 -05:00
import (
"fmt"
"git.linuxhg.com/Go_Training/magazine"
)
2023-12-20 22:01:29 -05:00
2024-01-16 10:21:20 -05:00
func printInfo(s *magazine.Subscriber) {
fmt.Println("Name:", s.Name)
fmt.Println("Monthly rate:", s.Rate)
fmt.Println("Active?:", s.Active)
2023-12-20 22:01:29 -05:00
}
2024-01-16 10:21:20 -05:00
func defaultSubscriber(name string) *magazine.Subscriber {
s := magazine.Subscriber{
Name: name,
Rate: 5.99,
Active: true,
}
s.Street = "123 Oak St."
s.City = "Omaha"
s.State = "NE"
s.PostalCode = "68111"
return &s
}
2023-12-20 22:01:29 -05:00
2024-01-16 10:21:20 -05:00
// accept a point to modify original struct
func applyDiscount(s *magazine.Subscriber) {
s.Rate = 4.99
}
func magazineSubscribers() {
employee := magazine.Employee{Name: "Joy Carr", Salary: 60000}
fmt.Println(employee.Name)
fmt.Println(employee.Salary)
subscriber := defaultSubscriber("Aman Singh")
applyDiscount(subscriber)
printInfo(subscriber)
fmt.Println(subscriber.State)
2023-12-20 22:01:29 -05:00
2024-01-16 10:21:20 -05:00
subscriber2 := defaultSubscriber("Beth Ryan")
printInfo(subscriber2)
2023-12-20 22:01:29 -05:00
}