Skip to content

Commit

Permalink
Merge pull request unknwon#320 from appleboy/patch-6
Browse files Browse the repository at this point in the history
fix: coding style and file format for chapter 10.
  • Loading branch information
unknwon committed Feb 11, 2017
2 parents 79a18bf + ca79293 commit 27bfcb2
Show file tree
Hide file tree
Showing 19 changed files with 731 additions and 720 deletions.
30 changes: 15 additions & 15 deletions eBook/exercises/chapter_10/anonymous_struct.go
Original file line number Diff line number Diff line change
@@ -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}
}
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}
}
39 changes: 20 additions & 19 deletions eBook/exercises/chapter_10/celsius.go
Original file line number Diff line number Diff line change
@@ -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
73 changes: 37 additions & 36 deletions eBook/exercises/chapter_10/days.go
Original file line number Diff line number Diff line change
@@ -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
*/
51 changes: 26 additions & 25 deletions eBook/exercises/chapter_10/employee_salary.go
Original file line number Diff line number Diff line change
@@ -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
// 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
81 changes: 41 additions & 40 deletions eBook/exercises/chapter_10/inherit_methods.go
Original file line number Diff line number Diff line change
@@ -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
*/
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
*/
Loading

0 comments on commit 27bfcb2

Please sign in to comment.