go-kata
Getting Started
Setup
- Set up Go.
- Create directory
$GOPATH/src/github.com/anniehedgpeth
- Run
git clone https://github.com/anniehedgpeth/go-kata.git
Every time you start the kata
- Close slack and email
- Set a timer for 30 minutes
- Create a new branch
20190104-annie
- Check into that branch
- Never merge to master
- When the timer goes off, quit
Start off using Visual Studio Code
Graduate to Go Land and use the refactorings to speed up. Only use the keyboard.
Background
Use this background information for the kata.
Simpson family:
name | birthday | favorite color | allowance |
---|---|---|---|
Homer | 22-September | blue | 10.50 |
Marge | 14-February | red | 20.34 |
Lisa | 4-July | purple | 15.50 |
Bart | 1-June | black | 5.50 |
Maggie | 1-April | white | 6.50 |
Kata
Basics
This gets you through most of the basic elements of golang.
After every step run the program and see the result
- Create a program in the
main
package (and folder) that outputsHello, World!
- (Variables) Extract
World
into a local variablename
- (Variables) Change the name to your name. It should now say, for example
Hello, Annie!
- (Functions) Extract the greeting into a function called
sayHi
that takes a name parameter and returns nothing. - (Packages) Make the variable name a package variable
- (If statement) In the
sayHi
function, if the name is literallyAnnie
addYour name is like the owner of this repo!
to the end of the greeting - (Functions returning value) Extract whether name is literally
Annie
expression to anisOwner
method returning abool
- (Loops) After the greeting (
sayHi
), in themain
function output a list of the names of members of the Simpson family (above), using the classicfor i := 0 ...
loop, where the output contains the index of the family member, for example:0: Marge
followed by1: Homer
- (Loops) Refactor this loop to use the
range
keyword - (Loops) Drop the index variable and refactor this to use the
range
keyword omitting the index. At this point you will output only the names, for example,Homer
thenMarge
- (Maps) Change your list to include the favorite color of each family member by their name (see above). Output it like this:
Marge: red
followed byHomer: blue
. The ordering doesn't matter. When looping continue to use therange
keyword. - (Functions) Refactor this into a method called
sayFavoriteColors
that outputs the greeting - (Maps) Create a new list of family members with their day of the month number that they were born. Then output these, for example:
Homer: 22
followed byBart: 5
. (these numbers can be fictional) - (Functions) Refactor this into a method called
sayBirthdays
. At this point, yourmain
method should be like this:
func main() {
sayHi()
sayFavoriteColors()
sayBirthdays()
}
- (Maps) Create another list that tracks the weekly allowance of each family member and output this:
Michael: $20.50
- make sure you use the cents place for your values. - (Functions) Refactor this into a method
sayAllowance
- (Structs) Create a
person
struct that contains the person's name, favorite color, birthday number, and allowance. Refactor various lists into one global list with all of this information, and ensure your methods still work. - (Packages) Migrate your functions to a
greeting
package and call that package from yourmain
package - (Sort) Whenever you talk about the family in the above methods, refer to them in alphabetical order (make sure your original list is not in alphabetical order)
- (Imports, Methods) Whenever you refer to your family by name, refer to them by all-caps, like
HOMER: blue
- (Packages) Put the
SayHi
method and associated variable and the family-oriented greetings in different files in the samegreeting
package
Intermediate
Here we get into files and advanced object functions.
- (File/IO) Load in the information for the family from the comma-delimited list defined in
simpsons.csv
- (File/IO) Load which family is used based on an argument to the program. For example if you run
simpsons.csv
the program should use the simpsons data. If the program has the argumentjacksons.csv
then load the jacksons data. - (Channels, Mutex) If the program contains both files (
simpsons.csv jacksons.csv
), load them concurrently into one list. Make sure the list isn't accessed at the same time. - (Structs, File/IO,
flag
package) If you pass inpets pets.csv
, it should output the pets and their favorite toy. For example:Fido: bone
followed byArgyle: yarn
- (Interfaces) If you pass in
everyone simpsons.csv pets.csv
, it should output the people and pets as animals. For example:Homer,Marge,Bart,Lisa,Maggie,Fido,Argyle
- (Methods) Create
isRich()
method onperson
that will return true if their allowance is greater than10.50
. - (Anonymous functions) create
familyMembers(where func(person member) bool)
method to filter family members - (Anonymous functions) if you pass in
rich simpsons.csv
, output family members defined in the CSV that are rich
Advanced
This extends what you learned above to learn how to create a webserver API and client in golang.
- Remove method calls from
main()
method - (REST Server) Create a
webserver
package, that when callinghttp://localhost/sayHi
it returns the results of theSayHi
method in thegreeting
package - Run your webserver with the
server
argument - When passing in
webclient
, call the web server athttp://localhost/sayHi
and output the contents of the resulting message. - Create an endpoint for
sayFavoriteColors
and call it frommain
whenwebclient
flag is given - Create an endpoint for
sayBirthdays
and call it frommain
whenwebclient
flag is given - Create an endpoint for
sayAllowance
and call it frommain
whenwebclient
flag is given - (JSON Serialization) Create a
http://localhost/familyMembers
endpoint that returns back your family members, deserialized asperson
structs, and output them - (PUT API calls) Create an endpoint at
http://localhost/you
where aPUT
allows you to change the name in memory every time you callsayHi
. Note that the name is different. - (Errors) If someone sets the name to be blank,
setName
method should return an error and you should return an error HTTP response code - When starting the server pass in the csv file to load for the family, like:
server simpsons.csv
- (Templates) Create an html endpoint at
http://localhost/report
that returns back an HTML formatted list of all family members