Skip to content

Latest commit

 

History

History
146 lines (104 loc) · 4.78 KB

File metadata and controls

146 lines (104 loc) · 4.78 KB

Chapter 1 - Tutorial

1.2 - Command-Line Arguments

  1. Modify the echo program to also print os.Args[0], the name of the command that invoked it.

    Solution

    Sample output:

    $ go run exercise01.go this is a test
    /tmp/go-build601632563/b001/exe/exercise01 this is a test
    
  2. Modify the echo program to print the index and value of each of its arguments, one per line.

    Solution

    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
    
  3. 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 the time package, 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
    

1.3 - Finding Duplicate Lines

  1. Modify dup2 to print the names of all files in which each duplicated line occurs.

    Solution

1.4 - Animated GIFs

  1. Change the Lissajous program's color palette to green on black, for added authenticity. To create the web color #RRGGBB, use color.RGBA{0xRR, 0xGG, 0xBB, 0xff}, where each pair of hexadecimal digits represents the intensity of the reg, green, or blue component of the pixel.

    Solution Sample Output

  2. Modify the Lissajous progrm to produce images in multiple colors by adding more values to palette and then displaying them by changing the third argument to SetColorIndex in some interesting way.

    In this case, I updated the program to change the foreground color between red, green, and blue on each iteration.

    Solution Sample Output

1.5 - Fetching a URL

  1. The function call io.Copy(dst, src) reads from src and writes to dst. Use it instead of ioutil.ReadAll to copy the response body to os.Stdout without requiring a buffer large enough to hold the entire stream. Be sure to check the error result of io.Copy.

    Solution

  2. Modify fetch to add the prefix http:// to each argument URL if it is missing. You might want to use strings.HasPrefix.

    Solution

  3. Modify fetch to also print the HTTP status code, found in resp.Status.

    Solution

1.6 - Fetching URLs Concurrently

  1. Find a web site that produces a large amount of data. Investigate caching by running fetchall twice in succession to see whether the reported time changes much. Do you get the same content each time? Modify fetchall to 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.
    

Solution

  1. Try fetchall with longer argument lists, such as samples from the top million web sites available at alexa.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.
    

1.7 - A Web Server

  1. 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=20 sets the number of cycles to 20 instead of the default 5. Use the strconv.Atoi function to convert the string parameter into an integer. You can see its documentation with go 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.
    

Solution