@ -0,0 +1,18 @@ | |||
package main | |||
import ( | |||
"errors" | |||
"fmt" | |||
) | |||
var ( | |||
ErrInvalidLastName = errors.New("invalid last name") | |||
ErrInvalidRoutingNumber = errors.New("invalid routing number") | |||
) | |||
func main() { | |||
fmt.Println(ErrInvalidLastName) | |||
fmt.Println(ErrInvalidRoutingNumber) | |||
} |
@ -0,0 +1,70 @@ | |||
package main | |||
import ( | |||
"errors" | |||
"fmt" | |||
"strings" | |||
) | |||
type directDeposit struct { | |||
lastName string | |||
firstName string | |||
bankName string | |||
routingNumber int | |||
accountNumber int | |||
} | |||
var ( | |||
ErrInvalidLastName = errors.New("invalid last name") | |||
ErrInvalidRoutingNumber = errors.New("invalid routing number") | |||
) | |||
func (d *directDeposit) validateRoutingNumber() error { | |||
if d.routingNumber < 100 { | |||
return ErrInvalidRoutingNumber | |||
} | |||
return nil | |||
} | |||
func (d *directDeposit) validateLastName() error { | |||
d.lastName = strings.TrimSpace(d.lastName) | |||
if len(d.lastName) == 0 { | |||
return ErrInvalidLastName | |||
} | |||
return nil | |||
} | |||
func (d *directDeposit) report() { | |||
fmt.Println(strings.Repeat("*", 40)) | |||
fmt.Println("Last Name: ", d.lastName) | |||
fmt.Println("First Name: ", d.firstName) | |||
fmt.Println("Bank Name: ", d.bankName) | |||
fmt.Println("Routing Number:", d.routingNumber) | |||
fmt.Println("Account Number:", d.accountNumber) | |||
} | |||
func main() { | |||
dp := directDeposit{ | |||
"", | |||
"Abe", | |||
"XYZ Inc", | |||
17, | |||
1809, | |||
} | |||
if err := dp.validateRoutingNumber(); err != nil { | |||
fmt.Println(err) | |||
} | |||
if err := dp.validateLastName(); err != nil { | |||
fmt.Println(err) | |||
} | |||
dp.report() | |||
} |
@ -0,0 +1,68 @@ | |||
package main | |||
import ( | |||
"errors" | |||
"fmt" | |||
"strings" | |||
) | |||
type directDeposit struct { | |||
lastName string | |||
firstName string | |||
bankName string | |||
routingNumber int | |||
accountNumber int | |||
} | |||
var ( | |||
ErrInvalidLastName = errors.New("invalid last name") | |||
ErrInvalidRoutingNumber = errors.New("invalid routing number") | |||
) | |||
func (d *directDeposit) validateRoutingNumber() error { | |||
if d.routingNumber < 100 { | |||
panic(ErrInvalidRoutingNumber) | |||
} | |||
return nil | |||
} | |||
func (d *directDeposit) validateLastName() error { | |||
d.lastName = strings.TrimSpace(d.lastName) | |||
if len(d.lastName) == 0 { | |||
return ErrInvalidLastName | |||
} | |||
return nil | |||
} | |||
func (d *directDeposit) report() { | |||
fmt.Println(strings.Repeat("*", 40)) | |||
fmt.Println("Last Name: ", d.lastName) | |||
fmt.Println("First Name: ", d.firstName) | |||
fmt.Println("Bank Name: ", d.bankName) | |||
fmt.Println("Routing Number:", d.routingNumber) | |||
fmt.Println("Account Number:", d.accountNumber) | |||
} | |||
func main() { | |||
dp := directDeposit{ | |||
"", | |||
"Abe", | |||
"XYZ Inc", | |||
17, | |||
1809, | |||
} | |||
_ = dp.validateRoutingNumber() | |||
if err := dp.validateLastName(); err != nil { | |||
fmt.Println(err) | |||
} | |||
dp.report() | |||
} |
@ -0,0 +1,74 @@ | |||
package main | |||
import ( | |||
"errors" | |||
"fmt" | |||
"strings" | |||
) | |||
type directDeposit struct { | |||
lastName string | |||
firstName string | |||
bankName string | |||
routingNumber int | |||
accountNumber int | |||
} | |||
var ( | |||
ErrInvalidLastName = errors.New("invalid last name") | |||
ErrInvalidRoutingNumber = errors.New("invalid routing number") | |||
) | |||
func (d *directDeposit) validateRoutingNumber() error { | |||
defer func() { | |||
if err := recover(); err == ErrInvalidRoutingNumber { | |||
fmt.Println(err) | |||
} | |||
}() | |||
if d.routingNumber < 100 { | |||
panic(ErrInvalidRoutingNumber) | |||
} | |||
return nil | |||
} | |||
func (d *directDeposit) validateLastName() error { | |||
d.lastName = strings.TrimSpace(d.lastName) | |||
if len(d.lastName) == 0 { | |||
return ErrInvalidLastName | |||
} | |||
return nil | |||
} | |||
func (d *directDeposit) report() { | |||
fmt.Println(strings.Repeat("*", 40)) | |||
fmt.Println("Last Name: ", d.lastName) | |||
fmt.Println("First Name: ", d.firstName) | |||
fmt.Println("Bank Name: ", d.bankName) | |||
fmt.Println("Routing Number:", d.routingNumber) | |||
fmt.Println("Account Number:", d.accountNumber) | |||
} | |||
func main() { | |||
dp := directDeposit{ | |||
"", | |||
"Abe", | |||
"XYZ Inc", | |||
17, | |||
1809, | |||
} | |||
_ = dp.validateRoutingNumber() | |||
if err := dp.validateLastName(); err != nil { | |||
fmt.Println(err) | |||
} | |||
dp.report() | |||
} |
@ -0,0 +1,28 @@ | |||
package main | |||
import ( | |||
"errors" | |||
"fmt" | |||
) | |||
func main() { | |||
test() | |||
fmt.Println("This line will not get printed") | |||
} | |||
func test() { | |||
n := func() { | |||
fmt.Println("Defer in test") | |||
} | |||
defer n() | |||
msg := "good-bye" | |||
message(msg) | |||
} | |||
func message(msg string) { | |||
f := func() { | |||
fmt.Println("Defer in message func") | |||
} | |||
defer f() | |||
if msg == "good-bye" { | |||
panic(errors.New("something went wrong")) | |||
} | |||
} |
@ -0,0 +1,26 @@ | |||
package main | |||
import ( | |||
"errors" | |||
"fmt" | |||
) | |||
func main() { | |||
a() | |||
fmt.Println("This line will now get printed from main() function") | |||
} | |||
func a() { | |||
b("good-bye") | |||
fmt.Println("Back in function a()") | |||
} | |||
func b(msg string) { | |||
defer func() { | |||
if r := recover(); r != nil { | |||
fmt.Println("error in func b()", r) | |||
} | |||
}() | |||
if msg == "good-bye" { | |||
panic(errors.New("something went wrong")) | |||
} | |||
fmt.Print(msg) | |||
} |
@ -0,0 +1,17 @@ | |||
package main | |||
import "fmt" | |||
func main() { | |||
nums := []int{2, 4, 6, 8} | |||
total := 0 | |||
// for i := 0; i <= 10; i++ { | |||
for num, _ := range nums { | |||
total += num | |||
} | |||
fmt.Println("Total:", total) | |||
} |
@ -0,0 +1,16 @@ | |||
package main | |||
import "fmt" | |||
func main() { | |||
km := 2 | |||
// if km > 2 { | |||
if km >= 2 { | |||
fmt.Println("Take the car") | |||
} else { | |||
fmt.Println("Going to walk today") | |||
} | |||
} |
@ -0,0 +1,52 @@ | |||
package main | |||
import ( | |||
"errors" | |||
"fmt" | |||
) | |||
var ( | |||
ErrHourlyRate = errors.New("invalid hourly rate") | |||
ErrHoursWorked = errors.New("invalid hours worked per week") | |||
) | |||
func main() { | |||
pay, err := payDay(81, 50) | |||
if err != nil { | |||
fmt.Println(err) | |||
} | |||
pay, err = payDay(80, 5) | |||
if err != nil { | |||
fmt.Println(err) | |||
} | |||
pay, err = payDay(80, 50) | |||
if err != nil { | |||
fmt.Println(err) | |||
} | |||
fmt.Println(pay) | |||
} | |||
func payDay(hoursWorked, hourlyRate int) (int, error) { | |||
if hourlyRate < 10 || hourlyRate > 75 { | |||
return 0, ErrHourlyRate | |||
} | |||
if hoursWorked < 0 || hoursWorked > 80 { | |||
return 0, ErrHoursWorked | |||
} | |||
if hoursWorked > 40 { | |||
hoursOver := hoursWorked - 40 | |||
overTime := hoursOver * 2 | |||
regularPay := 40 * hourlyRate | |||
return regularPay + overTime, nil | |||
} | |||
return hoursWorked * hourlyRate, nil | |||
} |
@ -0,0 +1,43 @@ | |||
package main | |||
import ( | |||
"errors" | |||
"fmt" | |||
) | |||
var ( | |||
ErrHourlyRate = errors.New("invalid hourly rate") | |||
ErrHoursWorked = errors.New("invalid hours worked per week") | |||
) | |||
func main() { | |||
pay := payDay(81, 50) | |||
fmt.Println(pay) | |||
} | |||
func payDay(hoursWorked, hourlyRate int) int { | |||
report := func() { | |||
fmt.Printf("hoursWorked: %d\nhourlyRate: %d\n", hoursWorked, hourlyRate) | |||
} | |||
defer report() | |||
if hourlyRate < 10 || hourlyRate > 75 { | |||
panic(ErrHourlyRate) | |||
} | |||
if hoursWorked < 0 || hoursWorked > 80 { | |||
panic(ErrHoursWorked) | |||
} | |||
if hoursWorked > 40 { | |||
hoursOver := hoursWorked - 40 | |||
overTime := hoursOver * 2 | |||
regularPay := 40 * hourlyRate | |||
return regularPay + overTime | |||
} | |||
return hoursWorked * hourlyRate | |||
} |
@ -0,0 +1,55 @@ | |||
package main | |||
import ( | |||
"errors" | |||
"fmt" | |||
) | |||
var ( | |||
ErrHourlyRate = errors.New("invalid hourly rate") | |||
ErrHoursWorked = errors.New("invalid hours worked per week") | |||
) | |||
func main() { | |||
pay := payDay(100, 25) | |||
fmt.Println(pay) | |||
pay = payDay(100, 200) | |||
fmt.Println(pay) | |||
pay = payDay(60, 25) | |||
fmt.Println(pay) | |||
} | |||
func payDay(hoursWorked, hourlyRate int) int { | |||
defer func() { | |||
if r := recover(); r != nil { | |||
if r == ErrHourlyRate { | |||
fmt.Printf("hourly rate: %d -> err: %v\n\n", hourlyRate, r) | |||
} | |||
if r == ErrHoursWorked { | |||
fmt.Printf("hours worked: %d -> err: %v\n\n", hoursWorked, r) | |||
} | |||
} | |||
fmt.Printf("Pay was calculated based on:\nhours worked: %d\nhourly rate: %d\n", | |||
hoursWorked, hourlyRate) | |||
}() | |||
if hourlyRate < 10 || hourlyRate > 75 { | |||
panic(ErrHourlyRate) | |||
} | |||
if hoursWorked < 0 || hoursWorked > 80 { | |||
panic(ErrHoursWorked) | |||
} | |||
if hoursWorked > 40 { | |||
hoursOver := hoursWorked - 40 | |||
overTime := hoursOver * 2 | |||
regularPay := 40 * hourlyRate | |||
return regularPay + overTime | |||
} | |||
return hoursWorked * hourlyRate | |||
} |