fix: coding style and file format for chapter 10.

This commit is contained in:
Bo-Yi Wu
2017-02-11 12:28:43 +08:00
parent d9041c7fc3
commit ca79293078
19 changed files with 731 additions and 720 deletions

View File

@@ -3,9 +3,9 @@ package main
import "fmt"
type C struct {
x float32;
int;
string;
x float32
int
string
}
func main() {

View File

@@ -9,11 +9,12 @@ import (
type Celsius float64
func (c Celsius) String() string {
return "The temperature is: " + strconv.FormatFloat(float64(c),'f', 1, 32) + " °C"
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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -17,7 +17,3 @@ func main() {
for _ = range lst.Iter() {
}
}

View File

@@ -23,6 +23,7 @@ func main() {
v.Magic()
v.MoreMagic()
}
/* Output:
voodoo magic
base magic base magic

View File

@@ -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]

View File

@@ -10,29 +10,30 @@ type Person struct {
lastName string
}
func upPerson (p Person) {
func upPerson(p Person) {
p.firstName = strings.ToUpper(p.firstName)
p.lastName = strings.ToUpper(p.lastName)
}
func main() {
// 1- struct as a value type:
// 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:
// 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"}
// 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

View File

@@ -21,7 +21,7 @@ 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) {
func Scale(p *Point, s float64) (q Point) {
q.X = p.X * s
q.Y = p.Y * s
return
@@ -31,19 +31,19 @@ func main() {
p1 := new(Point)
p1.X = 3
p1.Y = 4
fmt.Printf("The length of the vector p1 is: %f\n", Abs(p1) )
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) )
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("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
*/

View File

@@ -39,13 +39,14 @@ func main() {
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() )
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("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

View File

@@ -12,7 +12,7 @@ func (r *Rectangle) Area() int {
}
func (r *Rectangle) Perimeter() int {
return 2* (r.length + r.width)
return 2 * (r.length + r.width)
}
func main() {
@@ -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

View File

@@ -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
@@ -24,9 +25,8 @@ func (st *Stack) Pop() int {
func (st Stack) String() string {
str := ""
for ix:=0; ix<st.ix; ix++ {
for ix := 0; ix < st.ix; ix++ {
str += "[" + strconv.Itoa(ix) + ":" + strconv.Itoa(st.data[ix]) + "] "
}
return str
}

View File

@@ -6,6 +6,7 @@ import (
)
const LIMIT = 4
type Stack [LIMIT]int
func main() {
@@ -46,8 +47,8 @@ func (st *Stack) Push(n int) {
// 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 {
for ix := len(st) - 1; ix >= 0; ix-- {
if v = st[ix]; v != 0 {
st[ix] = 0
return v
}

View File

@@ -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
@@ -52,7 +53,7 @@ func (st *Stack) Pop() int {
func (st Stack) String() string {
str := ""
for ix:=0; ix<st.ix; ix++ {
for ix := 0; ix < st.ix; ix++ {
str += "[" + strconv.Itoa(ix) + ":" + strconv.Itoa(st.data[ix]) + "] "
}
return str

View File

@@ -15,9 +15,9 @@ const (
CST TZ = -6 * HOUR
)
var timeZones = map[TZ]string { UTC:"Universal Greenwich time",
EST:"Eastern Standard time",
CST:"Central Standard time" }
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 {
@@ -33,6 +33,7 @@ func main() {
fmt.Println(0 * HOUR)
fmt.Println(-6 * HOUR)
}
/* Output:
Eastern Standard time
Universal Greenwich time

View File

@@ -9,11 +9,12 @@ type T struct {
}
func main() {
t := &T{ 7, -2.35, "abc\tdef" }
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"

View File

@@ -25,8 +25,8 @@ type VCard struct {
}
func main() {
addr1 := &Address{"Elfenstraat", 12, "", "", "2600", "Mechelen", "België" }
addr2 := &Address{"Heideland", 28, "", "", "2640", "Mortsel", "België" }
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
@@ -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: