package
0.0.0-20240731084147-8c2c48fecfe2
Repository: https://github.com/jimsyyap/golang_recipe.git
Documentation: pkg.go.dev
# README
What the Code Does
This Go program is designed to do the following:
- Fetch Web Pages: It grabs text from a bunch of web pages (specifically RFC documents, which are technical standards for the internet).
- Count Letters: It counts how many times each letter of the alphabet (a-z) appears in the text of all those web pages combined.
- Display Results: Finally, it neatly shows us the count for each letter.
The Thought Process
Here's how a programmer might approach writing this code:
-
Libraries: We need tools to:
- Fetch web pages: The
net/http
library is perfect for this. - Read data: The
io
library helps us handle the content of the web pages. - Manipulate strings: The
strings
library is essential for working with text. - Use goroutines: Goroutines allow us to run tasks concurrently. The
time
library allows us to pause the main goroutine to allow other goroutines to complete before printing out the results.
- Fetch web pages: The
-
Data Storage:
allLetters
: This string holds all the lowercase letters we're interested in.frequency
: This is a list (or "slice" in Go) to keep track of how many times each letter appears. We'll initialize this list to all zeroes.
-
Functions:
countLetters
: This function does the heavy lifting. It takes a web page URL and thefrequency
list. It fetches the web page, reads its text, converts everything to lowercase, and then goes through each character. If the character is a letter, it increases the count in thefrequency
list at the corresponding index (e.g., 'a' is at index 0, 'b' at index 1, and so on).main
: This is where the program starts.- It creates the
frequency
list. - It loops through a range of RFC document numbers (1000 to 1030), creating a URL for each document.
- It launches a
countLetters
function for each URL as a goroutine. Since goroutines are lightweight threads of execution, this allows for the code to make all of the web requests concurrently. - It then sleeps for 10 seconds. This allows the goroutines spawned to make the web requests to each have time to finish. Since each web request takes some time to complete, we don't want to print out the results until all of the
countLetters
calls have finished. - Finally, it iterates over the
allLetters
string and thefrequency
list together, printing out each letter along with its count.
- It creates the
Simplified Explanation
Imagine you have a big pile of books. You want to know how many times the letter "e" appears in all of them.
- You start with a blank notebook to keep track of the counts for each letter.
- You take one book at a time, read it, and count how many "e"s you find. You write that number down in your notebook.
- You do this for all the books in the pile.
- At the end, you look at your notebook, and it tells you how many times "e" (and every other letter) appeared in all the books combined.
That's essentially what this Go program is doing, but with web pages instead of books and using goroutines to make all of the web requests at the same time.