From ca79293078768245cd5fcde883a62c124972cef7 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sat, 11 Feb 2017 12:28:43 +0800 Subject: [PATCH] fix: coding style and file format for chapter 10. --- .../exercises/chapter_10/anonymous_struct.go | 30 ++-- eBook/exercises/chapter_10/celsius.go | 39 +++--- eBook/exercises/chapter_10/days.go | 73 +++++----- eBook/exercises/chapter_10/employee_salary.go | 51 +++---- eBook/exercises/chapter_10/inherit_methods.go | 81 +++++------ eBook/exercises/chapter_10/inheritance_car.go | 119 ++++++++-------- eBook/exercises/chapter_10/iteration_list.go | 42 +++--- eBook/exercises/chapter_10/magic.go | 59 ++++---- eBook/exercises/chapter_10/main_stack.go | 91 ++++++------ eBook/exercises/chapter_10/personex1.go | 81 +++++------ eBook/exercises/chapter_10/point.go | 98 ++++++------- eBook/exercises/chapter_10/point_methods.go | 109 +++++++-------- eBook/exercises/chapter_10/rectangle.go | 57 ++++---- .../chapter_10/stack/stack_struct.go | 64 ++++----- eBook/exercises/chapter_10/stack_arr.go | 129 +++++++++--------- eBook/exercises/chapter_10/stack_struct.go | 119 ++++++++-------- eBook/exercises/chapter_10/timezones.go | 81 +++++------ eBook/exercises/chapter_10/type_string.go | 39 +++--- eBook/exercises/chapter_10/vcard.go | 89 ++++++------ 19 files changed, 731 insertions(+), 720 deletions(-) diff --git a/eBook/exercises/chapter_10/anonymous_struct.go b/eBook/exercises/chapter_10/anonymous_struct.go index 57f281c..4769fba 100755 --- a/eBook/exercises/chapter_10/anonymous_struct.go +++ b/eBook/exercises/chapter_10/anonymous_struct.go @@ -1,15 +1,15 @@ -package main - -import "fmt" - -type C struct { -x float32; -int; -string; -} - -func main() { - c := C{3.14, 7, "hello"} - fmt.Println(c.x, c.int, c.string) // output: 3.14 7 hello - fmt.Println(c) // output: {3.14 7 hello} -} \ No newline at end of file +package main + +import "fmt" + +type C struct { + x float32 + int + string +} + +func main() { + c := C{3.14, 7, "hello"} + fmt.Println(c.x, c.int, c.string) // output: 3.14 7 hello + fmt.Println(c) // output: {3.14 7 hello} +} diff --git a/eBook/exercises/chapter_10/celsius.go b/eBook/exercises/chapter_10/celsius.go index 64ae03c..50b4c71 100755 --- a/eBook/exercises/chapter_10/celsius.go +++ b/eBook/exercises/chapter_10/celsius.go @@ -1,19 +1,20 @@ -// celsius.go -package main - -import ( - "fmt" - "strconv" -) - -type Celsius float64 - -func (c Celsius) String() string { - return "The temperature is: " + strconv.FormatFloat(float64(c),'f', 1, 32) + " °C" -} - -func main() { - var c Celsius = 18.36 - fmt.Println(c) -} -// The temperature is: 18.4 °C +// celsius.go +package main + +import ( + "fmt" + "strconv" +) + +type Celsius float64 + +func (c Celsius) String() string { + return "The temperature is: " + strconv.FormatFloat(float64(c), 'f', 1, 32) + " °C" +} + +func main() { + var c Celsius = 18.36 + fmt.Println(c) +} + +// The temperature is: 18.4 °C diff --git a/eBook/exercises/chapter_10/days.go b/eBook/exercises/chapter_10/days.go index 8b606fc..a08cc58 100755 --- a/eBook/exercises/chapter_10/days.go +++ b/eBook/exercises/chapter_10/days.go @@ -1,36 +1,37 @@ -package main - -import "fmt" - -type Day int - -const ( - MO Day = iota - TU - WE - TH - FR - SA - SU -) - -var dayName = []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"} - -func (day Day) String() string { - return dayName[day] -} - -func main() { - var th Day = 3 - fmt.Printf("The 3rd day is: %s\n", th) - // If index > 6: panic: runtime error: index out of range - // but use the enumerated type to work with valid values: - var day = SU; - fmt.Println(day); // prints Sunday - fmt.Println(0, MO, 1, TU) -} -/* Output: -The 3rd day is: Thursday -Sunday -0 Monday 1 Tuesday -*/ +package main + +import "fmt" + +type Day int + +const ( + MO Day = iota + TU + WE + TH + FR + SA + SU +) + +var dayName = []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"} + +func (day Day) String() string { + return dayName[day] +} + +func main() { + var th Day = 3 + fmt.Printf("The 3rd day is: %s\n", th) + // If index > 6: panic: runtime error: index out of range + // but use the enumerated type to work with valid values: + var day = SU + fmt.Println(day) // prints Sunday + fmt.Println(0, MO, 1, TU) +} + +/* Output: +The 3rd day is: Thursday +Sunday +0 Monday 1 Tuesday +*/ diff --git a/eBook/exercises/chapter_10/employee_salary.go b/eBook/exercises/chapter_10/employee_salary.go index af1713b..050a2e6 100755 --- a/eBook/exercises/chapter_10/employee_salary.go +++ b/eBook/exercises/chapter_10/employee_salary.go @@ -1,25 +1,26 @@ -// methods1.go -package main - -import "fmt" - -/* basic data structure upon with we'll define methods */ -type employee struct { - salary float32 -} - -/* a method which will add a specified percent to an - employees salary */ -func (this *employee) giveRaise(pct float32) { - this.salary += this.salary * pct -} - -func main() { - /* create an employee instance */ - var e = new(employee) - e.salary = 100000; - /* call our method */ - e.giveRaise(0.04) - fmt.Printf("Employee now makes %f", e.salary) -} -// Employee now makes 104000.000000 \ No newline at end of file +// methods1.go +package main + +import "fmt" + +/* basic data structure upon with we'll define methods */ +type employee struct { + salary float32 +} + +/* a method which will add a specified percent to an + employees salary */ +func (this *employee) giveRaise(pct float32) { + this.salary += this.salary * pct +} + +func main() { + /* create an employee instance */ + var e = new(employee) + e.salary = 100000 + /* call our method */ + e.giveRaise(0.04) + fmt.Printf("Employee now makes %f", e.salary) +} + +// Employee now makes 104000.000000 diff --git a/eBook/exercises/chapter_10/inherit_methods.go b/eBook/exercises/chapter_10/inherit_methods.go index 256fbff..8e4700d 100755 --- a/eBook/exercises/chapter_10/inherit_methods.go +++ b/eBook/exercises/chapter_10/inherit_methods.go @@ -1,40 +1,41 @@ -package main - -import "fmt" - -type Base struct { - id string -} - -func (b *Base) Id() string { - return b.id -} - -func (b *Base) SetId(id string) { - b.id = id -} - -type Person struct { - Base - FirstName string - LastName string -} - -type Employee struct { - Person - salary float32 -} - -func main() { - idjb := Base{"007"} - jb := Person{idjb, "James", "Bond"} - e := &Employee{jb, 100000.} - fmt.Printf("ID of our hero: %v\n", e.Id()) - // Change the id: - e.SetId("007B") - fmt.Printf("The new ID of our hero: %v\n", e.Id()) -} -/* Output: -ID of our hero: 007 -The new ID of our hero: 007B -*/ \ No newline at end of file +package main + +import "fmt" + +type Base struct { + id string +} + +func (b *Base) Id() string { + return b.id +} + +func (b *Base) SetId(id string) { + b.id = id +} + +type Person struct { + Base + FirstName string + LastName string +} + +type Employee struct { + Person + salary float32 +} + +func main() { + idjb := Base{"007"} + jb := Person{idjb, "James", "Bond"} + e := &Employee{jb, 100000.} + fmt.Printf("ID of our hero: %v\n", e.Id()) + // Change the id: + e.SetId("007B") + fmt.Printf("The new ID of our hero: %v\n", e.Id()) +} + +/* Output: +ID of our hero: 007 +The new ID of our hero: 007B +*/ diff --git a/eBook/exercises/chapter_10/inheritance_car.go b/eBook/exercises/chapter_10/inheritance_car.go index 1858aac..8266fbe 100755 --- a/eBook/exercises/chapter_10/inheritance_car.go +++ b/eBook/exercises/chapter_10/inheritance_car.go @@ -1,59 +1,60 @@ -// inheritance_car.go -package main - -import ( - "fmt" -) - -type Engine interface { - Start() - Stop() -} - -type Car struct { - wheelCount int - Engine -} - -// define a behavior for Car -func (car Car) numberOfWheels() int { - return car.wheelCount -} - -type Mercedes struct { - Car //anonymous field Car -} - -// a behavior only available for the Mercedes -func (m *Mercedes) sayHiToMerkel() { - fmt.Println("Hi Angela!") -} - -func (c *Car) Start() { - fmt.Println("Car is started") -} - -func (c *Car) Stop() { - fmt.Println("Car is stopped") -} - -func (c *Car) GoToWorkIn() { - // get in car - c.Start(); - // drive to work - c.Stop(); - // get out of car -} - -func main() { - m := Mercedes{Car{4, nil}} - fmt.Println("A Mercedes has this many wheels: ", m.numberOfWheels()) - m.GoToWorkIn() - m.sayHiToMerkel() -} -/* Output: -A Mercedes has this many wheels: 4 -Car is started -Car is stopped -Hi Angela! -*/ +// inheritance_car.go +package main + +import ( + "fmt" +) + +type Engine interface { + Start() + Stop() +} + +type Car struct { + wheelCount int + Engine +} + +// define a behavior for Car +func (car Car) numberOfWheels() int { + return car.wheelCount +} + +type Mercedes struct { + Car //anonymous field Car +} + +// a behavior only available for the Mercedes +func (m *Mercedes) sayHiToMerkel() { + fmt.Println("Hi Angela!") +} + +func (c *Car) Start() { + fmt.Println("Car is started") +} + +func (c *Car) Stop() { + fmt.Println("Car is stopped") +} + +func (c *Car) GoToWorkIn() { + // get in car + c.Start() + // drive to work + c.Stop() + // get out of car +} + +func main() { + m := Mercedes{Car{4, nil}} + fmt.Println("A Mercedes has this many wheels: ", m.numberOfWheels()) + m.GoToWorkIn() + m.sayHiToMerkel() +} + +/* Output: +A Mercedes has this many wheels: 4 +Car is started +Car is stopped +Hi Angela! +*/ diff --git a/eBook/exercises/chapter_10/iteration_list.go b/eBook/exercises/chapter_10/iteration_list.go index 75b6f58..9b88c54 100755 --- a/eBook/exercises/chapter_10/iteration_list.go +++ b/eBook/exercises/chapter_10/iteration_list.go @@ -1,23 +1,19 @@ -/* -iteration_list.go:12: cannot define new methods on non-local type list.List -iteration_list.go:17: lst.Iter undefined (type *list.List has no field or method Iter) ----- Build file exited with code 1 -*/ -package main - -import "container/list" - -// cannot define new methods on non-local type list.List -// List iterator: -func (p *list.List) Iter() { -} - -func main() { - lst := new(list.List) - for _ = range lst.Iter() { - } -} - - - - +/* +iteration_list.go:12: cannot define new methods on non-local type list.List +iteration_list.go:17: lst.Iter undefined (type *list.List has no field or method Iter) +---- Build file exited with code 1 +*/ +package main + +import "container/list" + +// cannot define new methods on non-local type list.List +// List iterator: +func (p *list.List) Iter() { +} + +func main() { + lst := new(list.List) + for _ = range lst.Iter() { + } +} diff --git a/eBook/exercises/chapter_10/magic.go b/eBook/exercises/chapter_10/magic.go index 2cd7d1a..a208783 100755 --- a/eBook/exercises/chapter_10/magic.go +++ b/eBook/exercises/chapter_10/magic.go @@ -1,29 +1,30 @@ -// magic.go -package main - -import "fmt" - -type Base struct{} - -func (Base) Magic() { fmt.Print("base magic ") } - -func (self Base) MoreMagic() { - self.Magic() - self.Magic() -} - -type Voodoo struct { - Base -} - -func (Voodoo) Magic() { fmt.Println("voodoo magic") } - -func main() { - v := new(Voodoo) - v.Magic() - v.MoreMagic() -} -/* Output: -voodoo magic -base magic base magic -*/ \ No newline at end of file +// magic.go +package main + +import "fmt" + +type Base struct{} + +func (Base) Magic() { fmt.Print("base magic ") } + +func (self Base) MoreMagic() { + self.Magic() + self.Magic() +} + +type Voodoo struct { + Base +} + +func (Voodoo) Magic() { fmt.Println("voodoo magic") } + +func main() { + v := new(Voodoo) + v.Magic() + v.MoreMagic() +} + +/* Output: +voodoo magic +base magic base magic +*/ diff --git a/eBook/exercises/chapter_10/main_stack.go b/eBook/exercises/chapter_10/main_stack.go index 2125005..fd32d7d 100755 --- a/eBook/exercises/chapter_10/main_stack.go +++ b/eBook/exercises/chapter_10/main_stack.go @@ -1,45 +1,46 @@ -// Q15.go -package main - -import ( - "fmt" - "./stack/stack" -) - -func main() { - st1 := new(stack.Stack) - fmt.Printf("%v\n", st1) - st1.Push(3) - fmt.Printf("%v\n", st1) - st1.Push(7) - fmt.Printf("%v\n", st1) - st1.Push(10) - fmt.Printf("%v\n", st1) - st1.Push(99) - fmt.Printf("%v\n", st1) - p := st1.Pop() - fmt.Printf("Popped %d\n", p) - fmt.Printf("%v\n", st1) - p = st1.Pop() - fmt.Printf("Popped %d\n", p) - fmt.Printf("%v\n", st1) - p = st1.Pop() - fmt.Printf("Popped %d\n", p) - fmt.Printf("%v\n", st1) - p = st1.Pop() - fmt.Printf("Popped %d\n", p) - fmt.Printf("%v\n", st1) -} -/* Output: -[0:3] -[0:3] [1:7] -[0:3] [1:7] [2:10] -[0:3] [1:7] [2:10] [3:99] -Popped 99 -[0:3] [1:7] [2:10] -Popped 10 -[0:3] [1:7] -Popped 7 -[0:3] -Popped 3 -*/ +// Q15.go +package main + +import ( + "./stack/stack" + "fmt" +) + +func main() { + st1 := new(stack.Stack) + fmt.Printf("%v\n", st1) + st1.Push(3) + fmt.Printf("%v\n", st1) + st1.Push(7) + fmt.Printf("%v\n", st1) + st1.Push(10) + fmt.Printf("%v\n", st1) + st1.Push(99) + fmt.Printf("%v\n", st1) + p := st1.Pop() + fmt.Printf("Popped %d\n", p) + fmt.Printf("%v\n", st1) + p = st1.Pop() + fmt.Printf("Popped %d\n", p) + fmt.Printf("%v\n", st1) + p = st1.Pop() + fmt.Printf("Popped %d\n", p) + fmt.Printf("%v\n", st1) + p = st1.Pop() + fmt.Printf("Popped %d\n", p) + fmt.Printf("%v\n", st1) +} + +/* Output: +[0:3] +[0:3] [1:7] +[0:3] [1:7] [2:10] +[0:3] [1:7] [2:10] [3:99] +Popped 99 +[0:3] [1:7] [2:10] +Popped 10 +[0:3] [1:7] +Popped 7 +[0:3] +Popped 3 +*/ diff --git a/eBook/exercises/chapter_10/personex1.go b/eBook/exercises/chapter_10/personex1.go index df4e7ae..e237a06 100755 --- a/eBook/exercises/chapter_10/personex1.go +++ b/eBook/exercises/chapter_10/personex1.go @@ -1,40 +1,41 @@ -package main - -import ( - "fmt" - "strings" -) - -type Person struct { - firstName string - lastName string -} - -func upPerson (p Person) { - p.firstName = strings.ToUpper(p.firstName) - p.lastName = strings.ToUpper(p.lastName) -} - -func main() { -// 1- struct as a value type: - var pers1 Person - pers1.firstName = "Chris" - pers1.lastName = "Woodward" - upPerson(pers1) - fmt.Printf("The name of the person is %s %s\n", pers1.firstName, pers1.lastName) -// 2 - struct as a pointer: - pers2 := new(Person) - pers2.firstName = "Chris" - pers2.lastName = "Woodward" - upPerson(*pers2) - fmt.Printf("The name of the person is %s %s\n", pers2.firstName, pers2.lastName) -// 3 - struct as a literal: - pers3 := &Person{"Chris","Woodward"} - upPerson(*pers3) - fmt.Printf("The name of the person is %s %s\n", pers3.firstName, pers3.lastName) -} -/* Output: -The name of the person is Chris Woodward -The name of the person is Chris Woodward -The name of the person is Chris Woodward -*/ \ No newline at end of file +package main + +import ( + "fmt" + "strings" +) + +type Person struct { + firstName string + lastName string +} + +func upPerson(p Person) { + p.firstName = strings.ToUpper(p.firstName) + p.lastName = strings.ToUpper(p.lastName) +} + +func main() { + // 1- struct as a value type: + var pers1 Person + pers1.firstName = "Chris" + pers1.lastName = "Woodward" + upPerson(pers1) + fmt.Printf("The name of the person is %s %s\n", pers1.firstName, pers1.lastName) + // 2 - struct as a pointer: + pers2 := new(Person) + pers2.firstName = "Chris" + pers2.lastName = "Woodward" + upPerson(*pers2) + fmt.Printf("The name of the person is %s %s\n", pers2.firstName, pers2.lastName) + // 3 - struct as a literal: + pers3 := &Person{"Chris", "Woodward"} + upPerson(*pers3) + fmt.Printf("The name of the person is %s %s\n", pers3.firstName, pers3.lastName) +} + +/* Output: +The name of the person is Chris Woodward +The name of the person is Chris Woodward +The name of the person is Chris Woodward +*/ diff --git a/eBook/exercises/chapter_10/point.go b/eBook/exercises/chapter_10/point.go index a536cbe..a1530a5 100755 --- a/eBook/exercises/chapter_10/point.go +++ b/eBook/exercises/chapter_10/point.go @@ -1,49 +1,49 @@ -package main - -import ( - "fmt" - "math" -) - -type Point struct { - X, Y float64 -} - -type Point3 struct { - X, Y, Z float64 -} - -type Polar struct { - R, T float64 -} - -func Abs(p *Point) float64 { - return math.Sqrt(float64(p.X*p.X + p.Y*p.Y)) -} - -func Scale(p *Point,s float64) (q Point) { - q.X = p.X * s - q.Y = p.Y * s - return -} - -func main() { - p1 := new(Point) - p1.X = 3 - p1.Y = 4 - fmt.Printf("The length of the vector p1 is: %f\n", Abs(p1) ) - - p2:= &Point{4, 5} - fmt.Printf("The length of the vector p2 is: %f\n", Abs(p2) ) - - q := Scale(p1, 5) - fmt.Printf("The length of the vector q is: %f\n", Abs(&q) ) - fmt.Printf("Point p1 scaled by 5 has the following coordinates: X %f - Y %f", q.X, q.Y) -} -/* Output: -The length of the vector p1 is: 5.000000 -The length of the vector p2 is: 6.403124 -The length of the vector q is: 25.000000 -Point p1 scaled by 5 has the following coordinates: X 15.000000 - Y 20.000000 -*/ - +package main + +import ( + "fmt" + "math" +) + +type Point struct { + X, Y float64 +} + +type Point3 struct { + X, Y, Z float64 +} + +type Polar struct { + R, T float64 +} + +func Abs(p *Point) float64 { + return math.Sqrt(float64(p.X*p.X + p.Y*p.Y)) +} + +func Scale(p *Point, s float64) (q Point) { + q.X = p.X * s + q.Y = p.Y * s + return +} + +func main() { + p1 := new(Point) + p1.X = 3 + p1.Y = 4 + fmt.Printf("The length of the vector p1 is: %f\n", Abs(p1)) + + p2 := &Point{4, 5} + fmt.Printf("The length of the vector p2 is: %f\n", Abs(p2)) + + q := Scale(p1, 5) + fmt.Printf("The length of the vector q is: %f\n", Abs(&q)) + fmt.Printf("Point p1 scaled by 5 has the following coordinates: X %f - Y %f", q.X, q.Y) +} + +/* Output: +The length of the vector p1 is: 5.000000 +The length of the vector p2 is: 6.403124 +The length of the vector q is: 25.000000 +Point p1 scaled by 5 has the following coordinates: X 15.000000 - Y 20.000000 +*/ diff --git a/eBook/exercises/chapter_10/point_methods.go b/eBook/exercises/chapter_10/point_methods.go index fef05bb..f38a1f1 100755 --- a/eBook/exercises/chapter_10/point_methods.go +++ b/eBook/exercises/chapter_10/point_methods.go @@ -1,54 +1,55 @@ -// float64 is necessary as input to math.Sqrt() -package main - -import ( - "fmt" - "math" -) - -type Point struct { - X, Y float64 -} - -func (p *Point) Scale(s float64) { - p.X *= s - p.Y *= s -} - -func (p *Point) Abs() float64 { - return math.Sqrt(float64(p.X*p.X + p.Y*p.Y)) -} - -type Point3 struct { - X, Y, Z float64 -} - -func (p *Point3) Abs() float64 { - return math.Sqrt(float64(p.X*p.X + p.Y*p.Y + p.Z*p.Z)) -} - -type Polar struct { - R, T float64 -} - -func (p Polar) Abs() float64 { return p.R } - -func main() { - p1 := new(Point) - p1.X = 3 - p1.Y = 4 - fmt.Printf("The length of the vector p1 is: %f\n", p1.Abs()) - - p2:= &Point{4, 5} - fmt.Printf("The length of the vector p2 is: %f\n", p2.Abs() ) - - p1.Scale(5) - fmt.Printf("The length of the vector p1 after scaling is: %f\n", p1.Abs() ) - fmt.Printf("Point p1 after scaling has the following coordinates: X %f - Y %f", p1.X, p1.Y) -} -/* Output: -The length of the vector p1 is: 5.000000 -The length of the vector p2 is: 6.403124 -The length of the vector p1 after scaling is: 25.000000 -Point p1 after scaling has the following coordinates: X 15.000000 - Y 20.000000 -*/ \ No newline at end of file +// float64 is necessary as input to math.Sqrt() +package main + +import ( + "fmt" + "math" +) + +type Point struct { + X, Y float64 +} + +func (p *Point) Scale(s float64) { + p.X *= s + p.Y *= s +} + +func (p *Point) Abs() float64 { + return math.Sqrt(float64(p.X*p.X + p.Y*p.Y)) +} + +type Point3 struct { + X, Y, Z float64 +} + +func (p *Point3) Abs() float64 { + return math.Sqrt(float64(p.X*p.X + p.Y*p.Y + p.Z*p.Z)) +} + +type Polar struct { + R, T float64 +} + +func (p Polar) Abs() float64 { return p.R } + +func main() { + p1 := new(Point) + p1.X = 3 + p1.Y = 4 + fmt.Printf("The length of the vector p1 is: %f\n", p1.Abs()) + + p2 := &Point{4, 5} + fmt.Printf("The length of the vector p2 is: %f\n", p2.Abs()) + + p1.Scale(5) + fmt.Printf("The length of the vector p1 after scaling is: %f\n", p1.Abs()) + fmt.Printf("Point p1 after scaling has the following coordinates: X %f - Y %f", p1.X, p1.Y) +} + +/* Output: +The length of the vector p1 is: 5.000000 +The length of the vector p2 is: 6.403124 +The length of the vector p1 after scaling is: 25.000000 +Point p1 after scaling has the following coordinates: X 15.000000 - Y 20.000000 +*/ diff --git a/eBook/exercises/chapter_10/rectangle.go b/eBook/exercises/chapter_10/rectangle.go index 7f3a4b2..a81fad2 100755 --- a/eBook/exercises/chapter_10/rectangle.go +++ b/eBook/exercises/chapter_10/rectangle.go @@ -1,28 +1,29 @@ -// rectangle.go -package main - -import "fmt" - -type Rectangle struct { - length, width int -} - -func (r *Rectangle) Area() int { - return r.length * r.width -} - -func (r *Rectangle) Perimeter() int { - return 2* (r.length + r.width) -} - -func main() { - r1 := Rectangle{4, 3} - fmt.Println("Rectangle is: ", r1) - fmt.Println("Rectangle area is: ", r1.Area()) - fmt.Println("Rectangle perimeter is: ", r1.Perimeter()) -} -/* Output: -Rectangle is: {4 3} -Rectangle area is: 12 -Rectangle perimeter is: 14 -*/ \ No newline at end of file +// rectangle.go +package main + +import "fmt" + +type Rectangle struct { + length, width int +} + +func (r *Rectangle) Area() int { + return r.length * r.width +} + +func (r *Rectangle) Perimeter() int { + return 2 * (r.length + r.width) +} + +func main() { + r1 := Rectangle{4, 3} + fmt.Println("Rectangle is: ", r1) + fmt.Println("Rectangle area is: ", r1.Area()) + fmt.Println("Rectangle perimeter is: ", r1.Perimeter()) +} + +/* Output: +Rectangle is: {4 3} +Rectangle area is: 12 +Rectangle perimeter is: 14 +*/ diff --git a/eBook/exercises/chapter_10/stack/stack_struct.go b/eBook/exercises/chapter_10/stack/stack_struct.go index 08a58e4..643683f 100755 --- a/eBook/exercises/chapter_10/stack/stack_struct.go +++ b/eBook/exercises/chapter_10/stack/stack_struct.go @@ -1,32 +1,32 @@ -// stack_struct.go -package stack - -import "strconv" -const LIMIT = 10 - -type Stack struct { - ix int // first free position, so data[ix] == 0 - data [LIMIT]int -} - -func (st *Stack) Push(n int) { - if (st.ix + 1 > LIMIT) { - return // stack is full! - } - st.data[st.ix] = n - st.ix++ -} - -func (st *Stack) Pop() int { - st.ix-- - return st.data[st.ix] -} - -func (st Stack) String() string { - str := "" - for ix:=0; ix LIMIT { + return // stack is full! + } + st.data[st.ix] = n + st.ix++ +} + +func (st *Stack) Pop() int { + st.ix-- + return st.data[st.ix] +} + +func (st Stack) String() string { + str := "" + for ix := 0; ix < st.ix; ix++ { + str += "[" + strconv.Itoa(ix) + ":" + strconv.Itoa(st.data[ix]) + "] " + } + return str +} diff --git a/eBook/exercises/chapter_10/stack_arr.go b/eBook/exercises/chapter_10/stack_arr.go index ec628c9..27b21f6 100755 --- a/eBook/exercises/chapter_10/stack_arr.go +++ b/eBook/exercises/chapter_10/stack_arr.go @@ -1,64 +1,65 @@ -package main - -import ( - "fmt" - "strconv" -) - -const LIMIT = 4 -type Stack [LIMIT]int - -func main() { - st1 := new(Stack) - fmt.Printf("%v\n", st1) - st1.Push(3) - fmt.Printf("%v\n", st1) - st1.Push(7) - fmt.Printf("%v\n", st1) - st1.Push(10) - fmt.Printf("%v\n", st1) - st1.Push(99) - fmt.Printf("%v\n", st1) - p := st1.Pop() - fmt.Printf("Popped %d\n", p) - fmt.Printf("%v\n", st1) - p = st1.Pop() - fmt.Printf("Popped %d\n", p) - fmt.Printf("%v\n", st1) - p = st1.Pop() - fmt.Printf("Popped %d\n", p) - fmt.Printf("%v\n", st1) - p = st1.Pop() - fmt.Printf("Popped %d\n", p) - fmt.Printf("%v\n", st1) -} - -// put value on first position which contains 0, starting from bottom -func (st *Stack) Push(n int) { - for ix, v := range st { - if v == 0 { - st[ix] = n - break - } - } -} - -// take value from first position which contains !=0, starting from top -func (st *Stack) Pop() int { - v := 0 - for ix:= len(st)-1; ix>=0; ix-- { - if v=st[ix]; v!=0 { - st[ix] = 0 - return v - } - } - return 0 -} - -func (st Stack) String() string { - str := "" - for ix, v := range st { - str += "[" + strconv.Itoa(ix) + ":" + strconv.Itoa(v) + "] " - } - return str -} \ No newline at end of file +package main + +import ( + "fmt" + "strconv" +) + +const LIMIT = 4 + +type Stack [LIMIT]int + +func main() { + st1 := new(Stack) + fmt.Printf("%v\n", st1) + st1.Push(3) + fmt.Printf("%v\n", st1) + st1.Push(7) + fmt.Printf("%v\n", st1) + st1.Push(10) + fmt.Printf("%v\n", st1) + st1.Push(99) + fmt.Printf("%v\n", st1) + p := st1.Pop() + fmt.Printf("Popped %d\n", p) + fmt.Printf("%v\n", st1) + p = st1.Pop() + fmt.Printf("Popped %d\n", p) + fmt.Printf("%v\n", st1) + p = st1.Pop() + fmt.Printf("Popped %d\n", p) + fmt.Printf("%v\n", st1) + p = st1.Pop() + fmt.Printf("Popped %d\n", p) + fmt.Printf("%v\n", st1) +} + +// put value on first position which contains 0, starting from bottom +func (st *Stack) Push(n int) { + for ix, v := range st { + if v == 0 { + st[ix] = n + break + } + } +} + +// take value from first position which contains !=0, starting from top +func (st *Stack) Pop() int { + v := 0 + for ix := len(st) - 1; ix >= 0; ix-- { + if v = st[ix]; v != 0 { + st[ix] = 0 + return v + } + } + return 0 +} + +func (st Stack) String() string { + str := "" + for ix, v := range st { + str += "[" + strconv.Itoa(ix) + ":" + strconv.Itoa(v) + "] " + } + return str +} diff --git a/eBook/exercises/chapter_10/stack_struct.go b/eBook/exercises/chapter_10/stack_struct.go index 2a0ba11..179146b 100755 --- a/eBook/exercises/chapter_10/stack_struct.go +++ b/eBook/exercises/chapter_10/stack_struct.go @@ -1,59 +1,60 @@ -// stack_struct.go -package main - -import ( - "fmt" - "strconv" -) - -const LIMIT = 4 -type Stack struct { - ix int // first free position, so data[ix] == 0 - data [LIMIT]int -} - -func main() { - st1 := new(Stack) - fmt.Printf("%v\n", st1) - st1.Push(3) - fmt.Printf("%v\n", st1) - st1.Push(7) - fmt.Printf("%v\n", st1) - st1.Push(10) - fmt.Printf("%v\n", st1) - st1.Push(99) - fmt.Printf("%v\n", st1) - p := st1.Pop() - fmt.Printf("Popped %d\n", p) - fmt.Printf("%v\n", st1) - p = st1.Pop() - fmt.Printf("Popped %d\n", p) - fmt.Printf("%v\n", st1) - p = st1.Pop() - fmt.Printf("Popped %d\n", p) - fmt.Printf("%v\n", st1) - p = st1.Pop() - fmt.Printf("Popped %d\n", p) - fmt.Printf("%v\n", st1) -} - -func (st *Stack) Push(n int) { - if (st.ix + 1 > LIMIT) { - return // stack is full! - } - st.data[st.ix] = n - st.ix++ -} - -func (st *Stack) Pop() int { - st.ix-- - return st.data[st.ix] -} - -func (st Stack) String() string { - str := "" - for ix:=0; ix LIMIT { + return // stack is full! + } + st.data[st.ix] = n + st.ix++ +} + +func (st *Stack) Pop() int { + st.ix-- + return st.data[st.ix] +} + +func (st Stack) String() string { + str := "" + for ix := 0; ix < st.ix; ix++ { + str += "[" + strconv.Itoa(ix) + ":" + strconv.Itoa(st.data[ix]) + "] " + } + return str +} diff --git a/eBook/exercises/chapter_10/timezones.go b/eBook/exercises/chapter_10/timezones.go index 69d5444..3088b74 100755 --- a/eBook/exercises/chapter_10/timezones.go +++ b/eBook/exercises/chapter_10/timezones.go @@ -1,40 +1,41 @@ -// Output: -// Eastern Standard time -// Universal Greenwich time -// Central Standard time -package main - -import "fmt" - -type TZ int - -const ( - HOUR TZ = 60 * 60 - UTC TZ = 0 * HOUR - EST TZ = -5 * HOUR - CST TZ = -6 * HOUR -) - -var timeZones = map[TZ]string { UTC:"Universal Greenwich time", - EST:"Eastern Standard time", - CST:"Central Standard time" } - -func (tz TZ) String() string { // Method on TZ (not ptr) - for name, zone := range timeZones { - if tz == name { - return zone - } - } - return "" -} - -func main() { - fmt.Println(EST) // Print* knows about method String() of type TZ - fmt.Println(0 * HOUR) - fmt.Println(-6 * HOUR) -} -/* Output: -Eastern Standard time -Universal Greenwich time -Central Standard time -*/ \ No newline at end of file +// Output: +// Eastern Standard time +// Universal Greenwich time +// Central Standard time +package main + +import "fmt" + +type TZ int + +const ( + HOUR TZ = 60 * 60 + UTC TZ = 0 * HOUR + EST TZ = -5 * HOUR + CST TZ = -6 * HOUR +) + +var timeZones = map[TZ]string{UTC: "Universal Greenwich time", + EST: "Eastern Standard time", + CST: "Central Standard time"} + +func (tz TZ) String() string { // Method on TZ (not ptr) + for name, zone := range timeZones { + if tz == name { + return zone + } + } + return "" +} + +func main() { + fmt.Println(EST) // Print* knows about method String() of type TZ + fmt.Println(0 * HOUR) + fmt.Println(-6 * HOUR) +} + +/* Output: +Eastern Standard time +Universal Greenwich time +Central Standard time +*/ diff --git a/eBook/exercises/chapter_10/type_string.go b/eBook/exercises/chapter_10/type_string.go index 26fb70d..1804e11 100755 --- a/eBook/exercises/chapter_10/type_string.go +++ b/eBook/exercises/chapter_10/type_string.go @@ -1,19 +1,20 @@ -package main - -import "fmt" - -type T struct { - a int - b float32 - c string -} - -func main() { - t := &T{ 7, -2.35, "abc\tdef" } - fmt.Printf("%v\n", t) -} - -func (t *T) String() string { - return fmt.Sprintf("%d / %f / %q", t.a, t.b, t.c) -} -// Output: 7 / -2.350000 / "abc\tdef" +package main + +import "fmt" + +type T struct { + a int + b float32 + c string +} + +func main() { + t := &T{7, -2.35, "abc\tdef"} + fmt.Printf("%v\n", t) +} + +func (t *T) String() string { + return fmt.Sprintf("%d / %f / %q", t.a, t.b, t.c) +} + +// Output: 7 / -2.350000 / "abc\tdef" diff --git a/eBook/exercises/chapter_10/vcard.go b/eBook/exercises/chapter_10/vcard.go index 53d675c..3c21a83 100755 --- a/eBook/exercises/chapter_10/vcard.go +++ b/eBook/exercises/chapter_10/vcard.go @@ -1,44 +1,45 @@ -package main - -import ( - "fmt" - "time" -) - -type Address struct { - Street string - HouseNumber uint32 - HouseNumberAddOn string - POBox string - ZipCode string - City string - Country string -} - -type VCard struct { - FirstName string - LastName string - NickName string - BirtDate time.Time - Photo string - Addresses map[string]*Address -} - -func main() { - addr1 := &Address{"Elfenstraat", 12, "", "", "2600", "Mechelen", "België" } - addr2 := &Address{"Heideland", 28, "", "", "2640", "Mortsel", "België" } - addrs := make(map[string]*Address) - addrs["youth"] = addr1 - addrs["now"] = addr2 - birthdt := time.Date(1956, 1, 17, 15, 4, 5, 0, time.Local) - photo := "MyDocuments/MyPhotos/photo1.jpg" - vcard := &VCard{"Ivo", "Balbaert", "", birthdt, photo, addrs} - fmt.Printf("Here is the full VCard: %v\n", vcard) - fmt.Printf("My Addresses are:\n %v\n %v", addr1, addr2) -} -/* Output: -Here is the full VCard: &{Ivo Balbaert Sun Jan 17 15:04:05 +0000 1956 MyDocuments/MyPhotos/photo1.jpg map[now:0x126d57c0 youth:0x126d5500]} -My Addresses are: - &{Elfenstraat 12 2600 Mechelen België} - &{Heideland 28 2640 Mortsel België} -*/ +package main + +import ( + "fmt" + "time" +) + +type Address struct { + Street string + HouseNumber uint32 + HouseNumberAddOn string + POBox string + ZipCode string + City string + Country string +} + +type VCard struct { + FirstName string + LastName string + NickName string + BirtDate time.Time + Photo string + Addresses map[string]*Address +} + +func main() { + addr1 := &Address{"Elfenstraat", 12, "", "", "2600", "Mechelen", "België"} + addr2 := &Address{"Heideland", 28, "", "", "2640", "Mortsel", "België"} + addrs := make(map[string]*Address) + addrs["youth"] = addr1 + addrs["now"] = addr2 + birthdt := time.Date(1956, 1, 17, 15, 4, 5, 0, time.Local) + photo := "MyDocuments/MyPhotos/photo1.jpg" + vcard := &VCard{"Ivo", "Balbaert", "", birthdt, photo, addrs} + fmt.Printf("Here is the full VCard: %v\n", vcard) + fmt.Printf("My Addresses are:\n %v\n %v", addr1, addr2) +} + +/* Output: +Here is the full VCard: &{Ivo Balbaert Sun Jan 17 15:04:05 +0000 1956 MyDocuments/MyPhotos/photo1.jpg map[now:0x126d57c0 youth:0x126d5500]} +My Addresses are: + &{Elfenstraat 12 2600 Mechelen België} + &{Heideland 28 2640 Mortsel België} +*/