forked from unknwon/the-way-to-go_ZH_CN
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request unknwon#320 from appleboy/patch-6
fix: coding style and file format for chapter 10.
- Loading branch information
Showing
19 changed files
with
731 additions
and
720 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
*/ |
Oops, something went wrong.