mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-11-13 17:36:12 +08:00
fix: coding style and file format for chapter 10.
This commit is contained in:
@@ -3,9 +3,9 @@ package main
|
|||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
type C struct {
|
type C struct {
|
||||||
x float32;
|
x float32
|
||||||
int;
|
int
|
||||||
string;
|
string
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
@@ -16,4 +16,5 @@ func main() {
|
|||||||
var c Celsius = 18.36
|
var c Celsius = 18.36
|
||||||
fmt.Println(c)
|
fmt.Println(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
// The temperature is: 18.4 °C
|
// The temperature is: 18.4 °C
|
||||||
|
|||||||
@@ -25,10 +25,11 @@ func main() {
|
|||||||
fmt.Printf("The 3rd day is: %s\n", th)
|
fmt.Printf("The 3rd day is: %s\n", th)
|
||||||
// If index > 6: panic: runtime error: index out of range
|
// If index > 6: panic: runtime error: index out of range
|
||||||
// but use the enumerated type to work with valid values:
|
// but use the enumerated type to work with valid values:
|
||||||
var day = SU;
|
var day = SU
|
||||||
fmt.Println(day); // prints Sunday
|
fmt.Println(day) // prints Sunday
|
||||||
fmt.Println(0, MO, 1, TU)
|
fmt.Println(0, MO, 1, TU)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Output:
|
/* Output:
|
||||||
The 3rd day is: Thursday
|
The 3rd day is: Thursday
|
||||||
Sunday
|
Sunday
|
||||||
|
|||||||
@@ -17,9 +17,10 @@ func (this *employee) giveRaise(pct float32) {
|
|||||||
func main() {
|
func main() {
|
||||||
/* create an employee instance */
|
/* create an employee instance */
|
||||||
var e = new(employee)
|
var e = new(employee)
|
||||||
e.salary = 100000;
|
e.salary = 100000
|
||||||
/* call our method */
|
/* call our method */
|
||||||
e.giveRaise(0.04)
|
e.giveRaise(0.04)
|
||||||
fmt.Printf("Employee now makes %f", e.salary)
|
fmt.Printf("Employee now makes %f", e.salary)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Employee now makes 104000.000000
|
// Employee now makes 104000.000000
|
||||||
@@ -34,6 +34,7 @@ func main() {
|
|||||||
e.SetId("007B")
|
e.SetId("007B")
|
||||||
fmt.Printf("The new ID of our hero: %v\n", e.Id())
|
fmt.Printf("The new ID of our hero: %v\n", e.Id())
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Output:
|
/* Output:
|
||||||
ID of our hero: 007
|
ID of our hero: 007
|
||||||
The new ID of our hero: 007B
|
The new ID of our hero: 007B
|
||||||
|
|||||||
@@ -39,9 +39,9 @@ func (c *Car) Stop() {
|
|||||||
|
|
||||||
func (c *Car) GoToWorkIn() {
|
func (c *Car) GoToWorkIn() {
|
||||||
// get in car
|
// get in car
|
||||||
c.Start();
|
c.Start()
|
||||||
// drive to work
|
// drive to work
|
||||||
c.Stop();
|
c.Stop()
|
||||||
// get out of car
|
// get out of car
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,6 +51,7 @@ func main() {
|
|||||||
m.GoToWorkIn()
|
m.GoToWorkIn()
|
||||||
m.sayHiToMerkel()
|
m.sayHiToMerkel()
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Output:
|
/* Output:
|
||||||
A Mercedes has this many wheels: 4
|
A Mercedes has this many wheels: 4
|
||||||
Car is started
|
Car is started
|
||||||
|
|||||||
@@ -17,7 +17,3 @@ func main() {
|
|||||||
for _ = range lst.Iter() {
|
for _ = range lst.Iter() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ func main() {
|
|||||||
v.Magic()
|
v.Magic()
|
||||||
v.MoreMagic()
|
v.MoreMagic()
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Output:
|
/* Output:
|
||||||
voodoo magic
|
voodoo magic
|
||||||
base magic base magic
|
base magic base magic
|
||||||
|
|||||||
@@ -2,8 +2,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"./stack/stack"
|
"./stack/stack"
|
||||||
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -30,6 +30,7 @@ func main() {
|
|||||||
fmt.Printf("Popped %d\n", p)
|
fmt.Printf("Popped %d\n", p)
|
||||||
fmt.Printf("%v\n", st1)
|
fmt.Printf("%v\n", st1)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Output:
|
/* Output:
|
||||||
[0:3]
|
[0:3]
|
||||||
[0:3] [1:7]
|
[0:3] [1:7]
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ func main() {
|
|||||||
upPerson(*pers3)
|
upPerson(*pers3)
|
||||||
fmt.Printf("The name of the person is %s %s\n", pers3.firstName, pers3.lastName)
|
fmt.Printf("The name of the person is %s %s\n", pers3.firstName, pers3.lastName)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Output:
|
/* 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
|
The name of the person is Chris Woodward
|
||||||
|
|||||||
@@ -40,10 +40,10 @@ func main() {
|
|||||||
fmt.Printf("The length of the vector q is: %f\n", Abs(&q))
|
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)
|
fmt.Printf("Point p1 scaled by 5 has the following coordinates: X %f - Y %f", q.X, q.Y)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Output:
|
/* Output:
|
||||||
The length of the vector p1 is: 5.000000
|
The length of the vector p1 is: 5.000000
|
||||||
The length of the vector p2 is: 6.403124
|
The length of the vector p2 is: 6.403124
|
||||||
The length of the vector q is: 25.000000
|
The length of the vector q is: 25.000000
|
||||||
Point p1 scaled by 5 has the following coordinates: X 15.000000 - Y 20.000000
|
Point p1 scaled by 5 has the following coordinates: X 15.000000 - Y 20.000000
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ func main() {
|
|||||||
fmt.Printf("The length of the vector p1 after scaling is: %f\n", p1.Abs())
|
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)
|
fmt.Printf("Point p1 after scaling has the following coordinates: X %f - Y %f", p1.X, p1.Y)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Output:
|
/* Output:
|
||||||
The length of the vector p1 is: 5.000000
|
The length of the vector p1 is: 5.000000
|
||||||
The length of the vector p2 is: 6.403124
|
The length of the vector p2 is: 6.403124
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ func main() {
|
|||||||
fmt.Println("Rectangle area is: ", r1.Area())
|
fmt.Println("Rectangle area is: ", r1.Area())
|
||||||
fmt.Println("Rectangle perimeter is: ", r1.Perimeter())
|
fmt.Println("Rectangle perimeter is: ", r1.Perimeter())
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Output:
|
/* Output:
|
||||||
Rectangle is: {4 3}
|
Rectangle is: {4 3}
|
||||||
Rectangle area is: 12
|
Rectangle area is: 12
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
package stack
|
package stack
|
||||||
|
|
||||||
import "strconv"
|
import "strconv"
|
||||||
|
|
||||||
const LIMIT = 10
|
const LIMIT = 10
|
||||||
|
|
||||||
type Stack struct {
|
type Stack struct {
|
||||||
@@ -10,7 +11,7 @@ type Stack struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (st *Stack) Push(n int) {
|
func (st *Stack) Push(n int) {
|
||||||
if (st.ix + 1 > LIMIT) {
|
if st.ix+1 > LIMIT {
|
||||||
return // stack is full!
|
return // stack is full!
|
||||||
}
|
}
|
||||||
st.data[st.ix] = n
|
st.data[st.ix] = n
|
||||||
@@ -29,4 +30,3 @@ func (st Stack) String() string {
|
|||||||
}
|
}
|
||||||
return str
|
return str
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const LIMIT = 4
|
const LIMIT = 4
|
||||||
|
|
||||||
type Stack [LIMIT]int
|
type Stack [LIMIT]int
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const LIMIT = 4
|
const LIMIT = 4
|
||||||
|
|
||||||
type Stack struct {
|
type Stack struct {
|
||||||
ix int // first free position, so data[ix] == 0
|
ix int // first free position, so data[ix] == 0
|
||||||
data [LIMIT]int
|
data [LIMIT]int
|
||||||
@@ -38,7 +39,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (st *Stack) Push(n int) {
|
func (st *Stack) Push(n int) {
|
||||||
if (st.ix + 1 > LIMIT) {
|
if st.ix+1 > LIMIT {
|
||||||
return // stack is full!
|
return // stack is full!
|
||||||
}
|
}
|
||||||
st.data[st.ix] = n
|
st.data[st.ix] = n
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ func main() {
|
|||||||
fmt.Println(0 * HOUR)
|
fmt.Println(0 * HOUR)
|
||||||
fmt.Println(-6 * HOUR)
|
fmt.Println(-6 * HOUR)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Output:
|
/* Output:
|
||||||
Eastern Standard time
|
Eastern Standard time
|
||||||
Universal Greenwich time
|
Universal Greenwich time
|
||||||
|
|||||||
@@ -16,4 +16,5 @@ func main() {
|
|||||||
func (t *T) String() string {
|
func (t *T) String() string {
|
||||||
return fmt.Sprintf("%d / %f / %q", t.a, t.b, t.c)
|
return fmt.Sprintf("%d / %f / %q", t.a, t.b, t.c)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output: 7 / -2.350000 / "abc\tdef"
|
// Output: 7 / -2.350000 / "abc\tdef"
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ func main() {
|
|||||||
fmt.Printf("Here is the full VCard: %v\n", vcard)
|
fmt.Printf("Here is the full VCard: %v\n", vcard)
|
||||||
fmt.Printf("My Addresses are:\n %v\n %v", addr1, addr2)
|
fmt.Printf("My Addresses are:\n %v\n %v", addr1, addr2)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Output:
|
/* 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]}
|
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:
|
My Addresses are:
|
||||||
|
|||||||
Reference in New Issue
Block a user