Merge pull request #320 from appleboy/patch-6

fix: coding style and file format for chapter 10.
This commit is contained in:
无闻
2017-02-10 23:32:24 -05:00
committed by GitHub
19 changed files with 731 additions and 720 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,59 +1,60 @@
// inheritance_car.go
package main
import (
"fmt"
)
type Engine interface {
Start()
Stop()
}
type Car struct {
wheelCount int
Engine
}
// define a behavior for Car
func (car Car) numberOfWheels() int {
return car.wheelCount
}
type Mercedes struct {
Car //anonymous field Car
}
// a behavior only available for the Mercedes
func (m *Mercedes) sayHiToMerkel() {
fmt.Println("Hi Angela!")
}
func (c *Car) Start() {
fmt.Println("Car is started")
}
func (c *Car) Stop() {
fmt.Println("Car is stopped")
}
func (c *Car) GoToWorkIn() {
// get in car
c.Start();
// drive to work
c.Stop();
// get out of car
}
func main() {
m := Mercedes{Car{4, nil}}
fmt.Println("A Mercedes has this many wheels: ", m.numberOfWheels())
m.GoToWorkIn()
m.sayHiToMerkel()
}
/* Output:
A Mercedes has this many wheels: 4
Car is started
Car is stopped
Hi Angela!
*/
// inheritance_car.go
package main
import (
"fmt"
)
type Engine interface {
Start()
Stop()
}
type Car struct {
wheelCount int
Engine
}
// define a behavior for Car
func (car Car) numberOfWheels() int {
return car.wheelCount
}
type Mercedes struct {
Car //anonymous field Car
}
// a behavior only available for the Mercedes
func (m *Mercedes) sayHiToMerkel() {
fmt.Println("Hi Angela!")
}
func (c *Car) Start() {
fmt.Println("Car is started")
}
func (c *Car) Stop() {
fmt.Println("Car is stopped")
}
func (c *Car) GoToWorkIn() {
// get in car
c.Start()
// drive to work
c.Stop()
// get out of car
}
func main() {
m := Mercedes{Car{4, nil}}
fmt.Println("A Mercedes has this many wheels: ", m.numberOfWheels())
m.GoToWorkIn()
m.sayHiToMerkel()
}
/* Output:
A Mercedes has this many wheels: 4
Car is started
Car is stopped
Hi Angela!
*/

View File

@@ -1,23 +1,19 @@
/*
iteration_list.go:12: cannot define new methods on non-local type list.List
iteration_list.go:17: lst.Iter undefined (type *list.List has no field or method Iter)
---- Build file exited with code 1
*/
package main
import "container/list"
// cannot define new methods on non-local type list.List
// List iterator:
func (p *list.List) Iter() {
}
func main() {
lst := new(list.List)
for _ = range lst.Iter() {
}
}
/*
iteration_list.go:12: cannot define new methods on non-local type list.List
iteration_list.go:17: lst.Iter undefined (type *list.List has no field or method Iter)
---- Build file exited with code 1
*/
package main
import "container/list"
// cannot define new methods on non-local type list.List
// List iterator:
func (p *list.List) Iter() {
}
func main() {
lst := new(list.List)
for _ = range lst.Iter() {
}
}

View File

@@ -1,29 +1,30 @@
// magic.go
package main
import "fmt"
type Base struct{}
func (Base) Magic() { fmt.Print("base magic ") }
func (self Base) MoreMagic() {
self.Magic()
self.Magic()
}
type Voodoo struct {
Base
}
func (Voodoo) Magic() { fmt.Println("voodoo magic") }
func main() {
v := new(Voodoo)
v.Magic()
v.MoreMagic()
}
/* Output:
voodoo magic
base magic base magic
*/
// magic.go
package main
import "fmt"
type Base struct{}
func (Base) Magic() { fmt.Print("base magic ") }
func (self Base) MoreMagic() {
self.Magic()
self.Magic()
}
type Voodoo struct {
Base
}
func (Voodoo) Magic() { fmt.Println("voodoo magic") }
func main() {
v := new(Voodoo)
v.Magic()
v.MoreMagic()
}
/* Output:
voodoo magic
base magic base magic
*/

View File

@@ -1,45 +1,46 @@
// Q15.go
package main
import (
"fmt"
"./stack/stack"
)
func main() {
st1 := new(stack.Stack)
fmt.Printf("%v\n", st1)
st1.Push(3)
fmt.Printf("%v\n", st1)
st1.Push(7)
fmt.Printf("%v\n", st1)
st1.Push(10)
fmt.Printf("%v\n", st1)
st1.Push(99)
fmt.Printf("%v\n", st1)
p := st1.Pop()
fmt.Printf("Popped %d\n", p)
fmt.Printf("%v\n", st1)
p = st1.Pop()
fmt.Printf("Popped %d\n", p)
fmt.Printf("%v\n", st1)
p = st1.Pop()
fmt.Printf("Popped %d\n", p)
fmt.Printf("%v\n", st1)
p = st1.Pop()
fmt.Printf("Popped %d\n", p)
fmt.Printf("%v\n", st1)
}
/* Output:
[0:3]
[0:3] [1:7]
[0:3] [1:7] [2:10]
[0:3] [1:7] [2:10] [3:99]
Popped 99
[0:3] [1:7] [2:10]
Popped 10
[0:3] [1:7]
Popped 7
[0:3]
Popped 3
*/
// Q15.go
package main
import (
"./stack/stack"
"fmt"
)
func main() {
st1 := new(stack.Stack)
fmt.Printf("%v\n", st1)
st1.Push(3)
fmt.Printf("%v\n", st1)
st1.Push(7)
fmt.Printf("%v\n", st1)
st1.Push(10)
fmt.Printf("%v\n", st1)
st1.Push(99)
fmt.Printf("%v\n", st1)
p := st1.Pop()
fmt.Printf("Popped %d\n", p)
fmt.Printf("%v\n", st1)
p = st1.Pop()
fmt.Printf("Popped %d\n", p)
fmt.Printf("%v\n", st1)
p = st1.Pop()
fmt.Printf("Popped %d\n", p)
fmt.Printf("%v\n", st1)
p = st1.Pop()
fmt.Printf("Popped %d\n", p)
fmt.Printf("%v\n", st1)
}
/* Output:
[0:3]
[0:3] [1:7]
[0:3] [1:7] [2:10]
[0:3] [1:7] [2:10] [3:99]
Popped 99
[0:3] [1:7] [2:10]
Popped 10
[0:3] [1:7]
Popped 7
[0:3]
Popped 3
*/

View File

@@ -1,40 +1,41 @@
package main
import (
"fmt"
"strings"
)
type Person struct {
firstName string
lastName string
}
func upPerson (p Person) {
p.firstName = strings.ToUpper(p.firstName)
p.lastName = strings.ToUpper(p.lastName)
}
func main() {
// 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:
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"}
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
The name of the person is Chris Woodward
*/
package main
import (
"fmt"
"strings"
)
type Person struct {
firstName string
lastName string
}
func upPerson(p Person) {
p.firstName = strings.ToUpper(p.firstName)
p.lastName = strings.ToUpper(p.lastName)
}
func main() {
// 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:
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"}
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
The name of the person is Chris Woodward
*/

View File

@@ -1,49 +1,49 @@
package main
import (
"fmt"
"math"
)
type Point struct {
X, Y float64
}
type Point3 struct {
X, Y, Z float64
}
type Polar struct {
R, T float64
}
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) {
q.X = p.X * s
q.Y = p.Y * s
return
}
func main() {
p1 := new(Point)
p1.X = 3
p1.Y = 4
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) )
q := Scale(p1, 5)
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
*/
package main
import (
"fmt"
"math"
)
type Point struct {
X, Y float64
}
type Point3 struct {
X, Y, Z float64
}
type Polar struct {
R, T float64
}
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) {
q.X = p.X * s
q.Y = p.Y * s
return
}
func main() {
p1 := new(Point)
p1.X = 3
p1.Y = 4
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))
q := Scale(p1, 5)
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

@@ -1,54 +1,55 @@
// float64 is necessary as input to math.Sqrt()
package main
import (
"fmt"
"math"
)
type Point struct {
X, Y float64
}
func (p *Point) Scale(s float64) {
p.X *= s
p.Y *= s
}
func (p *Point) Abs() float64 {
return math.Sqrt(float64(p.X*p.X + p.Y*p.Y))
}
type Point3 struct {
X, Y, Z float64
}
func (p *Point3) Abs() float64 {
return math.Sqrt(float64(p.X*p.X + p.Y*p.Y + p.Z*p.Z))
}
type Polar struct {
R, T float64
}
func (p Polar) Abs() float64 { return p.R }
func main() {
p1 := new(Point)
p1.X = 3
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() )
p1.Scale(5)
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
The length of the vector p1 after scaling is: 25.000000
Point p1 after scaling has the following coordinates: X 15.000000 - Y 20.000000
*/
// float64 is necessary as input to math.Sqrt()
package main
import (
"fmt"
"math"
)
type Point struct {
X, Y float64
}
func (p *Point) Scale(s float64) {
p.X *= s
p.Y *= s
}
func (p *Point) Abs() float64 {
return math.Sqrt(float64(p.X*p.X + p.Y*p.Y))
}
type Point3 struct {
X, Y, Z float64
}
func (p *Point3) Abs() float64 {
return math.Sqrt(float64(p.X*p.X + p.Y*p.Y + p.Z*p.Z))
}
type Polar struct {
R, T float64
}
func (p Polar) Abs() float64 { return p.R }
func main() {
p1 := new(Point)
p1.X = 3
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())
p1.Scale(5)
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
The length of the vector p1 after scaling is: 25.000000
Point p1 after scaling has the following coordinates: X 15.000000 - Y 20.000000
*/

View File

@@ -1,28 +1,29 @@
// rectangle.go
package main
import "fmt"
type Rectangle struct {
length, width int
}
func (r *Rectangle) Area() int {
return r.length * r.width
}
func (r *Rectangle) Perimeter() int {
return 2* (r.length + r.width)
}
func main() {
r1 := Rectangle{4, 3}
fmt.Println("Rectangle is: ", r1)
fmt.Println("Rectangle area is: ", r1.Area())
fmt.Println("Rectangle perimeter is: ", r1.Perimeter())
}
/* Output:
Rectangle is: {4 3}
Rectangle area is: 12
Rectangle perimeter is: 14
*/
// rectangle.go
package main
import "fmt"
type Rectangle struct {
length, width int
}
func (r *Rectangle) Area() int {
return r.length * r.width
}
func (r *Rectangle) Perimeter() int {
return 2 * (r.length + r.width)
}
func main() {
r1 := Rectangle{4, 3}
fmt.Println("Rectangle is: ", r1)
fmt.Println("Rectangle area is: ", r1.Area())
fmt.Println("Rectangle perimeter is: ", r1.Perimeter())
}
/* Output:
Rectangle is: {4 3}
Rectangle area is: 12
Rectangle perimeter is: 14
*/

View File

@@ -1,32 +1,32 @@
// stack_struct.go
package stack
import "strconv"
const LIMIT = 10
type Stack struct {
ix int // first free position, so data[ix] == 0
data [LIMIT]int
}
func (st *Stack) Push(n int) {
if (st.ix + 1 > LIMIT) {
return // stack is full!
}
st.data[st.ix] = n
st.ix++
}
func (st *Stack) Pop() int {
st.ix--
return st.data[st.ix]
}
func (st Stack) String() string {
str := ""
for ix:=0; ix<st.ix; ix++ {
str += "[" + strconv.Itoa(ix) + ":" + strconv.Itoa(st.data[ix]) + "] "
}
return str
}
// stack_struct.go
package stack
import "strconv"
const LIMIT = 10
type Stack struct {
ix int // first free position, so data[ix] == 0
data [LIMIT]int
}
func (st *Stack) Push(n int) {
if st.ix+1 > LIMIT {
return // stack is full!
}
st.data[st.ix] = n
st.ix++
}
func (st *Stack) Pop() int {
st.ix--
return st.data[st.ix]
}
func (st Stack) String() string {
str := ""
for ix := 0; ix < st.ix; ix++ {
str += "[" + strconv.Itoa(ix) + ":" + strconv.Itoa(st.data[ix]) + "] "
}
return str
}

View File

@@ -1,64 +1,65 @@
package main
import (
"fmt"
"strconv"
)
const LIMIT = 4
type Stack [LIMIT]int
func main() {
st1 := new(Stack)
fmt.Printf("%v\n", st1)
st1.Push(3)
fmt.Printf("%v\n", st1)
st1.Push(7)
fmt.Printf("%v\n", st1)
st1.Push(10)
fmt.Printf("%v\n", st1)
st1.Push(99)
fmt.Printf("%v\n", st1)
p := st1.Pop()
fmt.Printf("Popped %d\n", p)
fmt.Printf("%v\n", st1)
p = st1.Pop()
fmt.Printf("Popped %d\n", p)
fmt.Printf("%v\n", st1)
p = st1.Pop()
fmt.Printf("Popped %d\n", p)
fmt.Printf("%v\n", st1)
p = st1.Pop()
fmt.Printf("Popped %d\n", p)
fmt.Printf("%v\n", st1)
}
// put value on first position which contains 0, starting from bottom
func (st *Stack) Push(n int) {
for ix, v := range st {
if v == 0 {
st[ix] = n
break
}
}
}
// 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 {
st[ix] = 0
return v
}
}
return 0
}
func (st Stack) String() string {
str := ""
for ix, v := range st {
str += "[" + strconv.Itoa(ix) + ":" + strconv.Itoa(v) + "] "
}
return str
}
package main
import (
"fmt"
"strconv"
)
const LIMIT = 4
type Stack [LIMIT]int
func main() {
st1 := new(Stack)
fmt.Printf("%v\n", st1)
st1.Push(3)
fmt.Printf("%v\n", st1)
st1.Push(7)
fmt.Printf("%v\n", st1)
st1.Push(10)
fmt.Printf("%v\n", st1)
st1.Push(99)
fmt.Printf("%v\n", st1)
p := st1.Pop()
fmt.Printf("Popped %d\n", p)
fmt.Printf("%v\n", st1)
p = st1.Pop()
fmt.Printf("Popped %d\n", p)
fmt.Printf("%v\n", st1)
p = st1.Pop()
fmt.Printf("Popped %d\n", p)
fmt.Printf("%v\n", st1)
p = st1.Pop()
fmt.Printf("Popped %d\n", p)
fmt.Printf("%v\n", st1)
}
// put value on first position which contains 0, starting from bottom
func (st *Stack) Push(n int) {
for ix, v := range st {
if v == 0 {
st[ix] = n
break
}
}
}
// 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 {
st[ix] = 0
return v
}
}
return 0
}
func (st Stack) String() string {
str := ""
for ix, v := range st {
str += "[" + strconv.Itoa(ix) + ":" + strconv.Itoa(v) + "] "
}
return str
}

View File

@@ -1,59 +1,60 @@
// stack_struct.go
package main
import (
"fmt"
"strconv"
)
const LIMIT = 4
type Stack struct {
ix int // first free position, so data[ix] == 0
data [LIMIT]int
}
func main() {
st1 := new(Stack)
fmt.Printf("%v\n", st1)
st1.Push(3)
fmt.Printf("%v\n", st1)
st1.Push(7)
fmt.Printf("%v\n", st1)
st1.Push(10)
fmt.Printf("%v\n", st1)
st1.Push(99)
fmt.Printf("%v\n", st1)
p := st1.Pop()
fmt.Printf("Popped %d\n", p)
fmt.Printf("%v\n", st1)
p = st1.Pop()
fmt.Printf("Popped %d\n", p)
fmt.Printf("%v\n", st1)
p = st1.Pop()
fmt.Printf("Popped %d\n", p)
fmt.Printf("%v\n", st1)
p = st1.Pop()
fmt.Printf("Popped %d\n", p)
fmt.Printf("%v\n", st1)
}
func (st *Stack) Push(n int) {
if (st.ix + 1 > LIMIT) {
return // stack is full!
}
st.data[st.ix] = n
st.ix++
}
func (st *Stack) Pop() int {
st.ix--
return st.data[st.ix]
}
func (st Stack) String() string {
str := ""
for ix:=0; ix<st.ix; ix++ {
str += "[" + strconv.Itoa(ix) + ":" + strconv.Itoa(st.data[ix]) + "] "
}
return str
}
// stack_struct.go
package main
import (
"fmt"
"strconv"
)
const LIMIT = 4
type Stack struct {
ix int // first free position, so data[ix] == 0
data [LIMIT]int
}
func main() {
st1 := new(Stack)
fmt.Printf("%v\n", st1)
st1.Push(3)
fmt.Printf("%v\n", st1)
st1.Push(7)
fmt.Printf("%v\n", st1)
st1.Push(10)
fmt.Printf("%v\n", st1)
st1.Push(99)
fmt.Printf("%v\n", st1)
p := st1.Pop()
fmt.Printf("Popped %d\n", p)
fmt.Printf("%v\n", st1)
p = st1.Pop()
fmt.Printf("Popped %d\n", p)
fmt.Printf("%v\n", st1)
p = st1.Pop()
fmt.Printf("Popped %d\n", p)
fmt.Printf("%v\n", st1)
p = st1.Pop()
fmt.Printf("Popped %d\n", p)
fmt.Printf("%v\n", st1)
}
func (st *Stack) Push(n int) {
if st.ix+1 > LIMIT {
return // stack is full!
}
st.data[st.ix] = n
st.ix++
}
func (st *Stack) Pop() int {
st.ix--
return st.data[st.ix]
}
func (st Stack) String() string {
str := ""
for ix := 0; ix < st.ix; ix++ {
str += "[" + strconv.Itoa(ix) + ":" + strconv.Itoa(st.data[ix]) + "] "
}
return str
}

View File

@@ -1,40 +1,41 @@
// Output:
// Eastern Standard time
// Universal Greenwich time
// Central Standard time
package main
import "fmt"
type TZ int
const (
HOUR TZ = 60 * 60
UTC TZ = 0 * HOUR
EST TZ = -5 * HOUR
CST TZ = -6 * HOUR
)
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 {
if tz == name {
return zone
}
}
return ""
}
func main() {
fmt.Println(EST) // Print* knows about method String() of type TZ
fmt.Println(0 * HOUR)
fmt.Println(-6 * HOUR)
}
/* Output:
Eastern Standard time
Universal Greenwich time
Central Standard time
*/
// Output:
// Eastern Standard time
// Universal Greenwich time
// Central Standard time
package main
import "fmt"
type TZ int
const (
HOUR TZ = 60 * 60
UTC TZ = 0 * HOUR
EST TZ = -5 * HOUR
CST TZ = -6 * HOUR
)
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 {
if tz == name {
return zone
}
}
return ""
}
func main() {
fmt.Println(EST) // Print* knows about method String() of type TZ
fmt.Println(0 * HOUR)
fmt.Println(-6 * HOUR)
}
/* Output:
Eastern Standard time
Universal Greenwich time
Central Standard time
*/

View File

@@ -1,19 +1,20 @@
package main
import "fmt"
type T struct {
a int
b float32
c string
}
func main() {
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"
package main
import "fmt"
type T struct {
a int
b float32
c string
}
func main() {
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

@@ -1,44 +1,45 @@
package main
import (
"fmt"
"time"
)
type Address struct {
Street string
HouseNumber uint32
HouseNumberAddOn string
POBox string
ZipCode string
City string
Country string
}
type VCard struct {
FirstName string
LastName string
NickName string
BirtDate time.Time
Photo string
Addresses map[string]*Address
}
func main() {
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
birthdt := time.Date(1956, 1, 17, 15, 4, 5, 0, time.Local)
photo := "MyDocuments/MyPhotos/photo1.jpg"
vcard := &VCard{"Ivo", "Balbaert", "", birthdt, photo, addrs}
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:
&{Elfenstraat 12 2600 Mechelen België}
&{Heideland 28 2640 Mortsel België}
*/
package main
import (
"fmt"
"time"
)
type Address struct {
Street string
HouseNumber uint32
HouseNumberAddOn string
POBox string
ZipCode string
City string
Country string
}
type VCard struct {
FirstName string
LastName string
NickName string
BirtDate time.Time
Photo string
Addresses map[string]*Address
}
func main() {
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
birthdt := time.Date(1956, 1, 17, 15, 4, 5, 0, time.Local)
photo := "MyDocuments/MyPhotos/photo1.jpg"
vcard := &VCard{"Ivo", "Balbaert", "", birthdt, photo, addrs}
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:
&{Elfenstraat 12 2600 Mechelen België}
&{Heideland 28 2640 Mortsel België}
*/