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"
|
||||
|
||||
type C struct {
|
||||
x float32;
|
||||
int;
|
||||
string;
|
||||
x float32
|
||||
int
|
||||
string
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
@@ -16,4 +16,5 @@ func main() {
|
||||
var c Celsius = 18.36
|
||||
fmt.Println(c)
|
||||
}
|
||||
|
||||
// The temperature is: 18.4 °C
|
||||
|
||||
@@ -25,10 +25,11 @@ func main() {
|
||||
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
|
||||
var day = SU
|
||||
fmt.Println(day) // prints Sunday
|
||||
fmt.Println(0, MO, 1, TU)
|
||||
}
|
||||
|
||||
/* Output:
|
||||
The 3rd day is: Thursday
|
||||
Sunday
|
||||
|
||||
@@ -17,9 +17,10 @@ func (this *employee) giveRaise(pct float32) {
|
||||
func main() {
|
||||
/* create an employee instance */
|
||||
var e = new(employee)
|
||||
e.salary = 100000;
|
||||
e.salary = 100000
|
||||
/* call our method */
|
||||
e.giveRaise(0.04)
|
||||
fmt.Printf("Employee now makes %f", e.salary)
|
||||
}
|
||||
|
||||
// Employee now makes 104000.000000
|
||||
@@ -34,6 +34,7 @@ func main() {
|
||||
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
|
||||
|
||||
@@ -39,9 +39,9 @@ func (c *Car) Stop() {
|
||||
|
||||
func (c *Car) GoToWorkIn() {
|
||||
// get in car
|
||||
c.Start();
|
||||
c.Start()
|
||||
// drive to work
|
||||
c.Stop();
|
||||
c.Stop()
|
||||
// get out of car
|
||||
}
|
||||
|
||||
@@ -51,6 +51,7 @@ func main() {
|
||||
m.GoToWorkIn()
|
||||
m.sayHiToMerkel()
|
||||
}
|
||||
|
||||
/* Output:
|
||||
A Mercedes has this many wheels: 4
|
||||
Car is started
|
||||
|
||||
@@ -17,7 +17,3 @@ func main() {
|
||||
for _ = range lst.Iter() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ func main() {
|
||||
v.Magic()
|
||||
v.MoreMagic()
|
||||
}
|
||||
|
||||
/* Output:
|
||||
voodoo magic
|
||||
base magic base magic
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"./stack/stack"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -30,6 +30,7 @@ func main() {
|
||||
fmt.Printf("Popped %d\n", p)
|
||||
fmt.Printf("%v\n", st1)
|
||||
}
|
||||
|
||||
/* Output:
|
||||
[0:3]
|
||||
[0:3] [1:7]
|
||||
|
||||
@@ -33,6 +33,7 @@ func main() {
|
||||
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
|
||||
|
||||
@@ -40,10 +40,10 @@ func main() {
|
||||
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
|
||||
*/
|
||||
|
||||
|
||||
@@ -46,6 +46,7 @@ func main() {
|
||||
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
|
||||
|
||||
@@ -21,6 +21,7 @@ func main() {
|
||||
fmt.Println("Rectangle area is: ", r1.Area())
|
||||
fmt.Println("Rectangle perimeter is: ", r1.Perimeter())
|
||||
}
|
||||
|
||||
/* Output:
|
||||
Rectangle is: {4 3}
|
||||
Rectangle area is: 12
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
package stack
|
||||
|
||||
import "strconv"
|
||||
|
||||
const LIMIT = 10
|
||||
|
||||
type Stack struct {
|
||||
@@ -10,7 +11,7 @@ type Stack struct {
|
||||
}
|
||||
|
||||
func (st *Stack) Push(n int) {
|
||||
if (st.ix + 1 > LIMIT) {
|
||||
if st.ix+1 > LIMIT {
|
||||
return // stack is full!
|
||||
}
|
||||
st.data[st.ix] = n
|
||||
@@ -29,4 +30,3 @@ func (st Stack) String() string {
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
)
|
||||
|
||||
const LIMIT = 4
|
||||
|
||||
type Stack [LIMIT]int
|
||||
|
||||
func main() {
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
)
|
||||
|
||||
const LIMIT = 4
|
||||
|
||||
type Stack struct {
|
||||
ix int // first free position, so data[ix] == 0
|
||||
data [LIMIT]int
|
||||
@@ -38,7 +39,7 @@ func main() {
|
||||
}
|
||||
|
||||
func (st *Stack) Push(n int) {
|
||||
if (st.ix + 1 > LIMIT) {
|
||||
if st.ix+1 > LIMIT {
|
||||
return // stack is full!
|
||||
}
|
||||
st.data[st.ix] = n
|
||||
|
||||
@@ -33,6 +33,7 @@ func main() {
|
||||
fmt.Println(0 * HOUR)
|
||||
fmt.Println(-6 * HOUR)
|
||||
}
|
||||
|
||||
/* Output:
|
||||
Eastern Standard time
|
||||
Universal Greenwich time
|
||||
|
||||
@@ -16,4 +16,5 @@ func main() {
|
||||
func (t *T) String() string {
|
||||
return fmt.Sprintf("%d / %f / %q", t.a, t.b, t.c)
|
||||
}
|
||||
|
||||
// Output: 7 / -2.350000 / "abc\tdef"
|
||||
|
||||
@@ -36,6 +36,7 @@ func main() {
|
||||
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:
|
||||
|
||||
Reference in New Issue
Block a user