From d29f680d0cc7f1b881802253d61cd46c9a5f47f2 Mon Sep 17 00:00:00 2001 From: John O'Keefe Date: Tue, 16 Jan 2024 10:21:20 -0500 Subject: [PATCH] finished chapter eight --- playground/magazineSubscribers.go | 50 +++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/playground/magazineSubscribers.go b/playground/magazineSubscribers.go index 693368e..089de05 100644 --- a/playground/magazineSubscribers.go +++ b/playground/magazineSubscribers.go @@ -1,22 +1,46 @@ // from chapter 8 of head first go package main -import "fmt" +import ( + "fmt" + "git.linuxhg.com/Go_Training/magazine" +) -type subscriberType struct { - name string - rate float64 - active bool +func printInfo(s *magazine.Subscriber) { + fmt.Println("Name:", s.Name) + fmt.Println("Monthly rate:", s.Rate) + fmt.Println("Active?:", s.Active) +} + +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 +} + +// 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) - var subscriber subscriberType - - subscriber.name = "Aman Singh" - subscriber.rate = 4.99 - subscriber.active = true - fmt.Println("Name:", subscriber.name) - fmt.Println("Monthly rate:", subscriber.rate) - fmt.Println("Active?:", subscriber.active) + subscriber2 := defaultSubscriber("Beth Ryan") + printInfo(subscriber2) }