trying_out_go/cmd/playground/magazineSubscribers.go

47 lines
942 B
Go

// from chapter 8 of head first go
package main
import (
"fmt"
"trying_out_go/pkg/magazine"
)
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)
subscriber2 := defaultSubscriber("Beth Ryan")
printInfo(subscriber2)
}