Skip to content

Commit e9ee508

Browse files
Updating templates.
1 parent 7794a0f commit e9ee508

File tree

12 files changed

+145
-151
lines changed

12 files changed

+145
-151
lines changed
Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,27 @@
11
// All material is licensed under the GNU Free Documentation License
22
// https://github.com/ArdanStudios/gotraining/blob/master/LICENSE
33

4-
// http://play.golang.org/p/BMOdLQfhdY
4+
// http://play.golang.org/p/Rj0QfwVPhX
55

66
// Declare a struct that represents a baseball player. Include name, atBats and hits.
77
// Declare a method that calculates a players batting average. The formula is Hits / AtBats.
88
// Declare a slice of this type and initalize the slice with several players. Iterate over
99
// the slice displaying the players name and batting average.
1010
package main
1111

12-
import "fmt"
12+
// Add imports.
1313

14-
// batter represents a playing in the game.
15-
type type_name struct {
16-
field_name type
17-
field_name type
18-
field_name type
14+
// Declare a struct that represents a ball player.
15+
// Include field called name, atBats and hits.
16+
17+
// Declare a method that calculates the batting average for a batter.
18+
func ( /* receiver */ ) average() /* return type */ {
1919
}
2020

2121
// main is the entry point for the application.
2222
func main() {
23-
// Create a few players.
24-
slice_name := []type_name{
25-
type_name{value, value, value},
26-
type_name{value, value, value},
27-
type_name{value, value, value},
28-
}
29-
30-
// Display the batting average for each player.
31-
for _, variable_name := range slice_name {
32-
fmt.Printf("%s: AVG[%.3f]\n", variable_name.field_name, variable_name.method_name())
33-
}
34-
}
23+
// Create a slice of players and populate each player
24+
// with field values.
3525

36-
// average calculates the batting average for a batter.
37-
func (receiver_name *type_name) method_name() return_type {
38-
return float64(receiver_name.field_name) / float64(receiver_name.field_name)
26+
// Display the batting average for each player in the slice.
3927
}

03-methods_interfaces_embedding/01-methods/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ http://www.goinggo.net/2014/05/methods-interfaces-and-embedded-types.html
3131

3232
Declare a struct that represents a baseball player. Include name, atBats and hits. Declare a method that calculates a players batting average. The formula is Hits / AtBats. Declare a slice of this type and initalize the slice with several players. Iterate over the slice displaying the players name and batting average.
3333

34-
[Template](exercises/template1/template1.go) ([Go Playground](http://play.golang.org/p/BMOdLQfhdY)) |
34+
[Template](exercises/template1/template1.go) ([Go Playground](http://play.golang.org/p/Rj0QfwVPhX)) |
3535
[Answer](exercises/exercise1/exercise1.go) ([Go Playground](http://play.golang.org/p/EZrIvPzfjh))
3636

3737
___

03-methods_interfaces_embedding/02-interfaces/exercises/exercise1/exercise1.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
// Declare a variable of type speaker and assign the _address of_ a value of type English
1111
// and call the method. Do it again for a value of type Chinese.
1212
//
13-
// From exercise 1, add a new function named sayHello that accepts a value of type speaker.
13+
// Add a new function named sayHello that accepts a value of type speaker.
1414
// Implement that function to call the sayHello method on the interface value. Then create
1515
// new values of each type and use the function.
1616
package main
@@ -42,7 +42,7 @@ func (c chinese) sayHello() {
4242

4343
// main is the entry point for the application.
4444
func main() {
45-
// Declare a variable of the interfafe type.
45+
// Declare a variable of the interface type.
4646
var sp speaker
4747

4848
// Assign a value to the interface type and
Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// All material is licensed under the GNU Free Documentation License
22
// https://github.com/ArdanStudios/gotraining/blob/master/LICENSE
33

4-
// http://play.golang.org/p/vQr6PHsYIn
4+
// http://play.golang.org/p/pKgSRPXBqC
55

66
// Declare an interface named speaker with a method named sayHello. Declare a struct
77
// named English that represents a person who speaks english and declare a struct named
@@ -10,57 +10,41 @@
1010
// Declare a variable of type speaker and assign the _address of_ a value of type English
1111
// and call the method. Do it again for a value of type Chinese.
1212
//
13-
// From exercise 1, add a new function named sayHello that accepts a value of type speaker.
13+
// Add a new function named sayHello that accepts a value of type speaker.
1414
// Implement that function to call the sayHello method on the interface value. Then create
1515
// new values of each type and use the function.
1616
package main
1717

18-
import "fmt"
18+
// Add imports.
1919

20-
// speaker implements the voice of anyone.
21-
type interface_type_name interface {
22-
method_name()
23-
}
20+
// Declare the speaker interface with a single method called sayHello.
2421

25-
// english represents an english speaking person.
26-
type english_type_name struct{}
22+
// Declare an empty struct type named english.
2723

28-
// sayHello implements the speaker interface.
29-
func (receiver_name english_type_name) method_name() {
30-
fmt.Println("Hello World")
31-
}
24+
// Declare a method named sayHello for the english type
25+
// using a value receiver. "Hello World"
26+
27+
// Declare an empty struct type named chinese.
3228

33-
// chinese represents a chinese speaking person.
34-
type chinese_type_name struct{}
29+
// Declare a method named sayHello for the chinese type
30+
// using a value receiver. "你好世界"
3531

36-
// sayHello implements the speaker interface.
37-
func (receiver_name chinese_type_name) method_name() {
38-
fmt.Println("你好世界")
32+
// sayHello accepts values of the interface type.
33+
func sayHello( /* Declare parameter */ ) {
34+
// Call the sayHello() method from the interface parameter.
3935
}
4036

4137
// main is the entry point for the application.
4238
func main() {
43-
// Declare a variable of the interfafe type.
44-
var variable_name interface_type_name
39+
// Declare a variable of the interface type set to its zero value.
4540

46-
// Assign a value to the interface type and
47-
// call the interface method.
48-
var variable_name english_type_name
49-
sp = variable_name
50-
sp.method_name()
41+
// Declare a variable of type english and assign it to
42+
// the interface variable.
43+
// Call the sayHello() method from the interface variable.
5144

52-
// Assign a different value to the interface type and
53-
// call the interface method.
54-
var variable_name chinese_type_name
55-
sp = variable_name
56-
sp.method_name()
57-
58-
// Create new values and call the function.
59-
function_name(new(english_type_name))
60-
function_name(new(chinese_type_name))
61-
}
45+
// Declare a variable of type chinese and assign it to
46+
// the interface variable.
47+
// Call the sayHello() method from the interface variable.
6248

63-
// SatHello abstracts speaking functionality.
64-
func function_name(variable_name interface_type_name) {
65-
variable_name.method_name()
49+
// Call the sayHello function passing each concrete type.
6650
}

03-methods_interfaces_embedding/02-interfaces/readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ http://www.goinggo.net/2014/05/methods-interfaces-and-embedded-types.html
3232

3333
**Part A** Declare an interface named Speaker with a method named SayHello. Declare a struct named English that represents a person who speaks english and declare a struct named Chinese for someone who speaks chinese. Implement the Speaker interface for each struct using a pointer receiver and these literal strings "Hello World" and "你好世界". Declare a variable of type Speaker and assign the _address of_ a value of type English and call the method. Do it again for a value of type Chinese.
3434

35-
**Part B** From exercise 1, add a new function named SayHello that accepts a value of type Speaker. Implement that function to call the SayHello method on the interface value. Then create new values of each type and use the function.
35+
**Part B** Add a new function named SayHello that accepts a value of type Speaker. Implement that function to call the SayHello method on the interface value. Then create new values of each type and use the function.
3636

37-
[Template](exercises/template1/template1.go) ([Go Playground](http://play.golang.org/p/vQr6PHsYIn)) |
37+
[Template](exercises/template1/template1.go) ([Go Playground](http://play.golang.org/p/pKgSRPXBqC)) |
3838
[Answer](exercises/exercise1/exercise1.go) ([Go Playground](http://play.golang.org/p/Kleh75YUx4))
3939

4040
___
Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// All material is licensed under the GNU Free Documentation License
22
// https://github.com/ArdanStudios/gotraining/blob/master/LICENSE
33

4-
// https://play.golang.org/p/aikeov9gA-
4+
// http://play.golang.org/p/a-Nzng_E6Z
55

66
// Declare a struct type named animal with two fields name and age. Declare a struct
77
// type named dog with the field bark. Embed the animal type into the dog type. Declare
@@ -17,54 +17,32 @@
1717
// bark field. Call the method yelp again from the value of type yelper.
1818
package main
1919

20-
import "fmt"
20+
// Add imports.
2121

22-
// yelper represents talking animals.
23-
type interface_type_name interface {
24-
method_name()
25-
}
22+
// Declare an interface type named yelper that has a
23+
// single method called yelp().
2624

27-
// animal represents all animals.
28-
type type_name struct {
29-
field_name type
30-
field_name type
31-
}
25+
// Declare a struct type named animal with two fields
26+
// name of type string and age of type in.
3227

33-
// yelp represents how an animal can speak.
34-
func (receiver_name *type_name) method_name() {
35-
fmt.Println("Not Implemented")
36-
}
28+
// Declare a method for the animal struct that implements
29+
// the yelper interface using a pointer receiver.
3730

38-
// dog represents a dog.
39-
type type_name struct {
40-
embedded_type_name
41-
field_name type_name
42-
}
31+
// Declare a struct type named dog that embeds the animal
32+
// type and has a field named bark of type int.
4333

44-
// yelp represents how an animal can speak.
45-
func (receiver_name *type_name) method_name() {
46-
for variable_name := 0; variable_name < receiver_name.field_name; variable_name++ {
47-
fmt.Print("BARK ")
48-
}
49-
fmt.Println()
50-
}
34+
// Declare a method for the dog struct that implements
35+
// the yelper interface using a pointer receiver.
5136

5237
// main is the entry point for the application.
5338
func main() {
54-
// Create a value of type dog.
55-
variable_name := type_name{
56-
embedded_type_name: embedded_type_name{
57-
field_name: value,
58-
field_name: 1,
59-
},
60-
field_name: value,
61-
}
39+
// Declare and initialize a variable of type dog.
40+
41+
// Display the value of the variable.
6242

63-
// Display the value.
64-
fmt.Println(variable_name)
43+
// Declare a variable of the yelper interface type.
44+
var y yelper
6545

66-
// Use the interface.
67-
var variable_name2 interface_type_name
68-
variable_name2 = &variable_name
69-
variable_name2.method_name()
46+
// Assign the dog variable to the iterface variable.
47+
// The call the interface method.
7048
}

03-methods_interfaces_embedding/03-embedding/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ http://www.goinggo.net/2014/05/methods-interfaces-and-embedded-types.html
3636

3737
**Part D** Implement the speaker interface for the dog type. Be creative with the bark field. Call the method yelp again from the value of type yelper.
3838

39-
[Template](exercises/template1/template1.go) ([Go Playground](https://play.golang.org/p/aikeov9gA-)) |
39+
[Template](exercises/template1/template1.go) ([Go Playground](http://play.golang.org/p/a-Nzng_E6Z)) |
4040
[Answer](exercises/exercise1/exercise1.go) ([Go Playground](http://play.golang.org/p/hvVA4zB9Bf))
4141

4242
___
Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
// All material is licensed under the GNU Free Documentation License
22
// https://github.com/ArdanStudios/gotraining/blob/master/LICENSE
33

4-
// Create a package named toy with a single unexported struct type named bat. Add
5-
// the exported fields Height and Weight to the bat type. Then create an exported
6-
// factory method called NewBat that returns pointers of type bat that are initialized
7-
// to their zero value.
4+
// Create a package named toy with a single exported struct type named Toy. Add
5+
// the exported fields Name and Weight. Then add two unexported fields named
6+
// onHand and sold. Declare a factory function called New to create values of
7+
// type toy and accept parameters for the exported fields. Then declare methods
8+
// that return and update values for the unexported fields.
89
//
9-
// Create a program that imports the toy package. Use the NewBat function to create a
10-
// value of bat and populate the values of Height and Width. Then display the value of
11-
// the bat variable.
10+
// Create a program that imports the toy package. Use the New function to create a
11+
// value of type toy. Then use the methods to set the counts and display the
12+
// field values of that toy value.
1213
package main
1314

1415
import (
@@ -19,11 +20,16 @@ import (
1920

2021
// main is the entry point for the application.
2122
func main() {
22-
// Create a value of type bat.
23-
bat := toy.NewBat()
24-
bat.Height = 28
25-
bat.Weight = 16
23+
// Create a value of type toy.
24+
toy := toy.New("Bat", 28)
2625

27-
// Display the value.
28-
fmt.Println(bat)
26+
// Update the counts.
27+
toy.UpdateOnHand(100)
28+
toy.UpdateSold(2)
29+
30+
// Display each field separately.
31+
fmt.Println("Name", toy.Name)
32+
fmt.Println("Weight", toy.Weight)
33+
fmt.Println("OnHand", toy.OnHand())
34+
fmt.Println("Sold", toy.Sold())
2935
}

04-packaging_exporting/exercises/exercise1/toy/toy.go

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,42 @@
44
// Package toy contains support for managing toy inventory.
55
package toy
66

7-
// bat represents the bat we sell.
8-
type bat struct {
9-
Height int
7+
// Toy represents a toy we sell.
8+
type Toy struct {
9+
Name string
1010
Weight int
11+
onHand int
12+
sold int
1113
}
1214

13-
// NewBat creates values of type bat.
14-
func NewBat() *bat {
15-
return new(bat)
15+
// New creates values of type toy.
16+
func New(name string, weight int) *Toy {
17+
return &Toy{
18+
Name: name,
19+
Weight: weight,
20+
}
21+
}
22+
23+
// OnHand returns the current number of this
24+
// toy on hand.
25+
func (t *Toy) OnHand() int {
26+
return t.onHand
27+
}
28+
29+
// UpdateOnHand updates the on hand count and
30+
// returns the current value.
31+
func (t *Toy) UpdateOnHand(count int) int {
32+
return t.onHand + count
33+
}
34+
35+
// Sold returns the current number of this
36+
// toy sold.
37+
func (t *Toy) Sold() int {
38+
return t.sold
39+
}
40+
41+
// UpdateSold updates the sold count and
42+
// returns the current value.
43+
func (t *Toy) UpdateSold(count int) int {
44+
return t.sold + count
1645
}

0 commit comments

Comments
 (0)