I have been slinging code since elementary school. When I was in sixth grade, we had our first coding class, where we wrote our first code. It was a simple HTML webpage. Nothing fancy. In fact, most of the projects we created back then looked like the MySpace profile of a 60 year-old lady. But it got me into coding, and after some time, I have become quite good at it. At the end of the year, we were supposed to create quite a complex project, and many of my classmates came back to me asking for help. For my services, I have received payments in cookies (I was basically a sugar whore back then) and still remember the satisfaction I got when I was able to provide help using my skills. This is why I will never shy away from learning new languages and technologies. This time, I have decided to go with Go.
What is Go?
Go, also known as Golang, is a popular programming language that was developed by Google in 2007. It was created to provide a simpler and more efficient way to write high-performance software. It’s statically typed, meaning that each variable and expression in a program has a specific data type that is determined at compile time. This means that the data type of a variable is determined before the program is run and remains fixed during runtime. It has a syntax that is similar to C. I have spent a lot of time coding in C, so this was quite a bonus for me as it made the whole learning process much easier. It’s fast, reliable, and, most importantly, secure. Go also has built-in support for concurrency and garbage collection, making the programmer’s life much easier. It’s a go to (pun intended) solution for building high-performance software systems that can handle large-scale applications with ease.
Where can one use Go?
There are several areas where Go can shine. One of them is web development, as its built-in support for concurrency and efficient memory management make it well-suited for building high-performance web apps. Many popular frameworks, such as Revel and Gin, are built using Go.
I am personally interested in Go from the Dev(Sec)Ops perspective. It can be used for building automation tools such as CI/CD pipelines, deployment scripts, and configuration management tools. In DevSecOps, it can be used for security tools and applications. Having a fast compilation speed and efficient memory management make it well-suited for building security tools that can scan large codebases for vulnerabilities and other security issues.
Last but not least, it can be used in the fields of data science and machine learning. Its simplicity and performance make it ideal for building data pipelines and implementing machine learning algorithms. Go is also used in the fields of blockchain and cryptocurrency.
My first Go project
Frankly, I breathe and fart articles every day. I am spending a lot of time each day reading articles. I often imply a tactic by reading the headings in an article and determining whether I should save the article to my Pocket reader for reading it later or not. This makes it an ideal first project for me, as I prefer to learn by doing and create something I can utilize. A simple web scraper that would grab all those headings for me would be great. So I have made one.
package main
import (
"fmt"
"log"
"os"
"net/http"
"github.com/PuerkitoBio/goquery"
)
func main() {
// Get the URL from the command-line argument
if len(os.Args) < 2 {
fmt.Println("Please provide a URL to scrape as an argument")
os.Exit(1)
}
url := os.Args[1]
scrape(url)
}
// Scrape function that takes a URL as a parameter and extracts all headings from its body
func scrape(url string) {
// Make HTTP request to the website
response, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
// Load HTML document
doc, err := goquery.NewDocumentFromReader(response.Body)
if err != nil {
log.Fatal(err)
}
// Find all heading elements within the body
headings := doc.Find("body").Find("h1, h2, h3, h4, h5, h6")
// Print out the heading text
headings.Each(func(i int, s *goquery.Selection) {
fmt.Printf("Heading %d: %s\n", i+1, s.Text())
})
}
You can see that I have used a goquery module in order to achieve my goal. In short, it provides us with features similar to those of jQuery, allowing me to easily grab all the elements I need.
Testing it
I have tested it on my buddy’s blog. Randall posts about programming, cybersecurity, and life (which are quite similar to the topics I wrote about). One of his posts that I have read recently was titled Please Stop Using Local Storage. And this is the output given by my script.
It has parsed all the headings and printed them out for me. I have written these scrapers in the past using Beautiful Soup, but compared to my new Go parser they are much slower (which is no surprise). Next time, I would like to enhance the current project into a more sophisticated tool (better presentation, more features, and quality of life improvements). Stay tuned for more updates!
If you have read so far, you might want to follow me here on Hashnode. Feel free to connect with me over at LinkedIn or Mastodon.