finished chapter eight
This commit is contained in:
parent
50853b19b4
commit
d29f680d0c
@ -1,22 +1,46 @@
|
|||||||
// from chapter 8 of head first go
|
// from chapter 8 of head first go
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"git.linuxhg.com/Go_Training/magazine"
|
||||||
|
)
|
||||||
|
|
||||||
type subscriberType struct {
|
func printInfo(s *magazine.Subscriber) {
|
||||||
name string
|
fmt.Println("Name:", s.Name)
|
||||||
rate float64
|
fmt.Println("Monthly rate:", s.Rate)
|
||||||
active bool
|
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() {
|
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
|
subscriber2 := defaultSubscriber("Beth Ryan")
|
||||||
|
printInfo(subscriber2)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user