Web Scraping Golang



In general programming interfaces are contracts that have a set of functions to be implemented to fulfill that contract. Go is no different. Go has great support for interfaces and they are implemented in an implicit way. They allow polymorphism in Go. In this post, we will talk about interfaces, what they are, and how they can be used.

Web Scraping Golang

Scraping framework for extracting the data you need from websites, used for a wide range of applications, like data mining, data processing or archiving. Javascript Promise.all is very powerful features of multithreading or asynchronous operations in multiple tasks processing. In this tutorial I'm doing web sc. Apr 17, 2021 Python language is more focused on writing web applications. Go needs more code to perform the same number of actions. Python needs fewer code compares to Go. 28.5 K Github stars: 67.5 K Github stars. Go developer ranges from approximately $64,089 per year: The average salary for a Python Developer is $120,359 per year in the United States.

What is an Interface?

An interface is an abstract concept which enables polymorphism in Go. A variable of that interface can hold the value that implements the type. Type assertion is used to get the underlying concrete value as we will see in this post.

Declaring an interface in GoLang

An interface is declared as a type. Here is the declaration that is used to declare an interface.

Golang Web Api

type interfaceName interface{}

Web Scraping is a process of extracting content from a website. Since any website is but HTML code, web scraper parses and extracts values from this underlying HTML code. Lets set things up to begin with. I create a folder named webscraper under the folder in which I.

Scraping webpage golang

Zero-value of an interface

The zero value of an interface is nil. That means it holds no value and type. The code below shows that.

The empty interface in Go

Web Scraping Golang

An interface is empty if it has no functions at all. An empty interface holds any type. That’s why it is extremely useful in many cases. Below is the declaration of an empty interface.

var i interface{}

Implementing an interface in GoLang

An interface is implemented when the type has implemented the functions of the interface. Here is an example showing how to implement an interface.

Implementing multiple interfaces in Go

Multiple interfaces can be implemented at the same time. If all the functions are all implemented then the type implements all the interfaces. Below the type, the bird type implements both the interfaces by implementing the functions.

Composing interfaces together

Interfaces can be composed together. The composition is one of the most important concepts in software development. When multiple interfaces are implemented then the type has performed composition. This is really helpful where polymorphism is needed.

Values in an interface

Interface values have a concrete value and a dynamic type.

Golang Web Development Tutorial

In the code above chirper is of type Bird but has a concrete value of {Chirpir}.

Type assertion using the interface

Type assertion is a way to get the underlying value an interface holds. This means if an interface variable is assigned a string then the underlying value it holds is the string. Here is an example showing how to use type assertion using interfaces.

Type switch using an interface

Type switches are an extremely similar control structure like the switch-cases, the only difference is here the interface type is used to switch between different conditions.

Equality of interface values

The interface values can be equal if any of the conditions shown below are true.

  • They both are nil.
  • They have the same underlying concrete values and the same dynamic type.

Using interfaces with functions

Interfaces can be passed to functions just like any other type. Here is an example showing the usage of the interface with functions. A great advantage when using an interface is that it allows any type of argument as we can see in this code below.

Uses of an interface

Interfaces are used in Go where polymorphism is needed. In a function where multiple types can be passed an interface can be used. Interfaces allow Go to have polymorphism.

Interfaces are a great feature in Go and should be used wisely.