finished chapter eight
This commit is contained in:
parent
50853b19b4
commit
d29f680d0c
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user