-
Modify the
echoprogram to also printos.Args[0], the name of the command that invoked it.Sample output:
$ go run exercise01.go this is a test /tmp/go-build601632563/b001/exe/exercise01 this is a test -
Modify the
echoprogram to print the index and value of each of its arguments, one per line.Sample output:
$ go run exercise02.go this is a test os.Args[0] = this os.Args[1] = is os.Args[2] = a os.Args[3] = test this is a test -
Experiment to measure the difference in running time between our potentially inefficient versions and the one that uses
strings.Join. (Section 1.6 illustrates part of thetimepackage, and Section 11.4 shows how to write benchmark tests for systematic performance evaulation.)Implemenetations Benchmark program
Sample run:
$ go test -bench=. -- $(for ((i = 0; i < 100; ++i)); do echo "arg${i}"; done) | grep -v arg goos: linux goarch: amd64 50000 24708 ns/op 200000 7977 ns/op PASS ok _/go-programming-language/Chapter01/exercise03 3.167s
-
Modify
dup2to print the names of all files in which each duplicated line occurs.
-
Change the Lissajous program's color palette to green on black, for added authenticity. To create the web color
#RRGGBB, usecolor.RGBA{0xRR, 0xGG, 0xBB, 0xff}, where each pair of hexadecimal digits represents the intensity of the reg, green, or blue component of the pixel. -
Modify the Lissajous progrm to produce images in multiple colors by adding more values to
paletteand then displaying them by changing the third argument toSetColorIndexin some interesting way.In this case, I updated the program to change the foreground color between red, green, and blue on each iteration.
-
The function call
io.Copy(dst, src)reads fromsrcand writes todst. Use it instead ofioutil.ReadAllto copy the response body toos.Stdoutwithout requiring a buffer large enough to hold the entire stream. Be sure to check the error result ofio.Copy. -
Modify
fetchto add the prefixhttp://to each argument URL if it is missing. You might want to usestrings.HasPrefix. -
Modify
fetchto also print the HTTP status code, found inresp.Status.
-
Find a web site that produces a large amount of data. Investigate caching by running
fetchalltwice in succession to see whether the reported time changes much. Do you get the same content each time? Modifyfetchallto print its output to a file so it can be examined.With websites that cache generated pages, the second run of `fetchall` results in faster response times. The solution here updates `fetchall` to write the responses to files, indexed by the initial start time and the site's offset into the argument array.
-
Try
fetchallwith longer argument lists, such as samples from the top million web sites available atalexa.com. How does the program behave if a web site just doesn't respond? (Section 8.9 describes mechanisms for coping in such cases.)If a web site doesn't respond (i.e., `fetchall` can establish a connection to the server, but the server doesn't respond with data), then the goroutine performing that operation will block indefinately, waiting for a response.
-
Modify the Lissajous server to read parameter values from the URL. For example, you might arrange it so that a URL like
http://localhost:8000/?cycles=20sets the number of cycles to 20 instead of the default 5. Use thestrconv.Atoifunction to convert the string parameter into an integer. You can see its documentation withgo doc strconv.Atoi.This version makes all of the previously constant values URL parameters, for example: http://localhost:8000/?size=300&res=0.002&cycles=10&nframes=16&delay=4 If there's a failure parsing the parameter, the default is used automatically.