#Task List
- Follow this link and press the Run button.
- Rewrite the program using the short declaration syntax; there should be no
var
declarations, only:=
.
- Follow this link for instructions.
- Declare a variable called
i
which is a slice of 5int
. - Declare a variable called
f
which is a slice of 9float64
. - Declare a variable called
s
which is a slice of 4string
. - Does your program print the expected result,
5
94
?
- Follow this link and complete the exercise.
- Follow this link and complete the exercise.
- Follow this link and complete the exercise.
- Follow this link and complete the exercise.
- If you cannot figure it out, don't worry, there is an answer on the next slide.
cd
$GOPATH/src/whattimeisit`- Build the program with,
go
build(if you make an error, go back and edit
main.go`) - Run your program
./whattimeisit
, it should print something like this
The current time is 2016-12-05 12:33:18.222821474 +0900 JST
- The code for this exercise is in
$GOPATH/src/simplestrings/
- Read the source code for
simplestrings.go
- Complete the exercise in
$GOPATH/src/exercises/methods
by making all the tests pass.
- Complete the exercise in
$GOPATH/src/exercises/pointers
by making the test pass.
This exercise asks you to write a function that reads lines from an io.Reader and returns a string containing all the lines read.
The code for this exercise is in $GOPATH/src/exercises/input
To familarise you with the io.Reader
implementations available in the io
package, this exercise is all about Readers.
- Complete the exercise in
$GOPATH/src/exercises/readers
by making the test pass. - If you get stuck, consult the documentation in the
io
package.
Now we know about io.Reader
's, error
's, we can write some more useful programs.
The code for this exercise is in $GOPATH/src/exercises/countlines
Note:
go
test` always executes from the package's source directory, this makes it simple to include fixtures for your tests.- The go tool ignores any directory called
testdata
, or starts with a.
or_
.
In the previous counting example, if an error happened, the program would exit.
In this exercise, we'll handle errors by returning them to the caller.
The code for this exercise is in $GOPATH/src/exercises/errorhandling
Let's turn out Countlines
function into a program.
The code for this exercise is in $GOPATH/src/exercises/linecount
Complete the program so it reads the number of lines sent to it via stdin.
cat testdata/moby.txt | ./linecount
22659
Let's extend our linecount
program to handle files passed on the command line.
The code for this exercise is in $GOPATH/src/exercises/countmanyfiles
Complete the program so it counts the lines in files passed via the command line.
./countmanyfiles testdata/*.txt
testdata/dracula.txt 15973
testdata/pride-and-prejudice.txt 13427
testdata/sherlock.txt 13052
In the previous example we used the shell to list files to process.
In this exercise, let's extend our countmanyfiles
program to walk the directory listing itself.
To do this we use the ReadDir
method on os.File
.
Note: Be careful to only read files, not directories, and do not read files that don't end in .txt
The code for this exercise is in $GOPATH/src/exercises/countdir
Complete the program so it counts the lines in files passed via the command line.
./countdir testdata/
testdata/christmas-carol.txt 4236
testdata/tom-sawyer.txt 9209
The Go standard library supports writing HTTP clients and servers with the net/http
package.
Using the net/http
package is very straight forward:
resp, err := http.Get("http://example.com/")
if err != nil {
// handle error
}
resp
is a http.Response
structure, which has lots of interesting fields.
Let's write a simple HTTP client that can fetch HTTP URLs.
./httpget http://httpbin.org/ip
{
"origin": "125.203.122.114"
}
The code for this exercise is in $GOPATH/src/exercises/httpget
The service at http://httpbin.org/
returns JSON bodies.
The encoding/json
package can decode JSON data into a map.
result := make(map[string]string)
dec := json.NewDecoder(resp.Body)
err := dec.Decode(&result)
if err != nil {
// handle error
}
Let's use this to write a program that will tell us our public IP address.
./whatismyip
My IP address is: 125.203.122.114
The code for this exercise is in $GOPATH/src/exercises/whatismyip
The encoding/json
package requires the fields of a struct to be public (start with an upper case letter), this means the keys in your JSON document will be upper case.
We can fix this and control the output of the JSON with a tag.
The format of the JSON tag is documented on the json.Encode
method.
The code for this exercise is in $GOPATH/src/exercises/jsonenc
Let's write a HTTP service that counts the lines in a book via http.
Features:
- When the client requests
/books/{book}
we look up the book and return the number of lines counted. - The response to the client should be in JSON format and include the number of lines and the title of the book.
- Book directory is configurable.
./httplinecount ../../../books/ &
[1] 17554
curl http://localhost:8080/books/moby.txt
{"title":"moby.txt","lines":"22659"}
The code for this exercise is in $GOPATH/src/exercises/httplinecount