INTRODUCTION TO GOLANG PROGRAMMING LANGUAGE

Cecilia Orji
7 min readApr 16, 2023

--

Outline:

  • A brief history of Golang
  • What is Golang used for?
  • Why GO? [merits and demerits]
  • Industries a golang dev can work
  • A simple calculator built using golang
  • Testing
  • Conclusion

Let us dive into it.

A Brief History of Golang

Go or Golang is a new language. Although it borrows ideas from existing languages, it has unusual properties that make its programs different in character from programs written in their relatives.

A straightforward translation of a C++ or Java program into Go is unlikely to produce a satisfactory result — Java programs are written in Java, not Go. On the other hand, thinking about the problem from a Go perspective could create a successful but quite different program.

In other words, to write Go well, it’s essential to understand its properties and idioms. It’s also important to know the established conventions for programming in Go, such as naming, formatting, and program construction.

So that the programs you write will be easy for other Go programmers to understand [Effective Go].

Go started in September 2007 when Robert Griesemer, Ken Thompson, and Rob Pike began discussing a new language to address the engineering challenges they and their colleagues at Google were facing in their daily work.

When it was first released to the public(made open-source) in November 2009, they didn’t know if the language would be widely adopted or if it might influence future languages. Looking back from 2020, Go has succeeded in both ways.

For example, it is widely used inside and outside Google, and its approaches to network concurrency and software engineering have had a noticeable effect on other languages and their tools [Using Go at Google].

What is Golang used for?

Golang’s support for concurrency makes it an excellent choice for handling bottlenecks that prevent scalability.

One particular feature of Go, goroutines, offers concurrency by allowing functions or methods to run concurrently with other functions or methods. A complimentary feature, channels, works to link goroutines together. To learn more about Concurrency in Go, listen to an explanation by Rob Pike.

Backend developers can reap the most benefits from Go. Using the language, developers can handle heavy requests on the server side with minuscule memory usage.

The Golang mascot called the Gopher

Some more specific examples of where Go can be used are;

  1. Distributed Network Services: Concurrency thrives in any networking environment. Using goroutines and channels, Go is naturally suited for developing network services such as APIs, web servers, and mini frameworks for web applications.
  2. Cloud-Native Development: Many stand-out cloud platforms like Kubernetes were built with Go. Google Cloud also uses Go to enhance scalability and performance.
  3. Replacements for Existing Infrastructure: Many old infrastructures were rewritten using Go to modernize what’s become obsolete over time.
  4. Utilities and Stand-Alone Tools: Since Go is fully functional with little to no dependencies, the language is well-suited for building small tooling items.
  5. News Outlets: The British Broadcasting Corporation, more commonly known as simply BBC, has been using Go on its backend for some time now.
  6. Media Platforms: Youtube, SoundCloud, and Netflix all run on Go.
  7. On-Demand Services: Uber uses Go to get to users in the nick of time for just about every request.

Why GO? [merits and demerits]

The merits are;

  1. Golang is fast: Golang is capable of compiling directly to machine code without using an interpreter. As a result, development is accelerated because no intermediary steps are required. Golang is always one step ahead of Java when it comes to execution speed. Golang-based programs are lightning-fast, and compilation is also quicker.
  2. The credentials: Google finances Golang. And it has some real geniuses as its creators, Robert Griesemer, Rob Pike, and Ken Thompson who are already known for their contributions to Unix, JVM, C, B, etc. Interestingly, the belief is that the language was built to combat the problems of the past and to anticipate future challenges.
  3. It is easy to learn: Another reason to use Golang is that it is easy to master. Go is simple for software developers, especially those with a solid understanding of C and Java.
  4. It is well-scaled: Go supports concurrent programming. Go language has Goroutines, which are functions that can run simultaneously and independently. Goroutines occupy just 2 KB of memory, making them adaptable when several concurrent processes are required to run. ​​Golang’s Goroutines are the exact opposite of what Java’s thread does: a heavyweight that eats up memory. It is technically possible to operate thousands of GoRoutines without the risk of crashing your system. Running more compact and efficient software can give you an edge over your rivals.
  5. Built-in testing: Go has a great testing framework built into the language itself, that can also generate code coverage reports; a great linter, and lots of other compelling tools to perform static analysis.
  6. Easy and clear documentation: Golang’s documentation is designed to be comprehensive, user-friendly, and accessible to developers of all skill levels. The language’s syntax and libraries are well-documented with clear explanations, examples, and tutorials, making it easy for developers to understand and implement Go code.

The demerits of Golang are;

Go is extremely young. Before its official release, the language was publicly announced in 2009 after being developed in 2007. Thus, the language is over a decade old. But this is relatively new compared to the most traditional languages like Java and Python, which were released in the 1990s.

The downside to this youth is that Go can be difficult to implement with older platforms that aren’t quite ready for it, so to speak. And programmers using the language may not be as well-adjusted either.

Other cons include too simple, the absence of manual memory management, runtime safety not that good, operator overloading, and problems in code duplication and metaprogramming because those cannot be statically checked.

  • Industries a Golang dev can work

Some of the companies that currently use Golang are;

  • Google
  • Twitch
  • SoundCloud
  • Netflix
  • BBC
  • Uber
  • SendGrid
  • Alibaba
  • American Express
  • PayPal
  • Solaris bank
  • Etc
  1. A golang developer can work on technology platforms. Technology Platforms provide a common set of functions that accelerate application development so that solutions can be deployed to the business more quickly. They often require high scalability and reliability which Golangs excels at. Example Dropbox, SendGrid, etc.
  2. Online Booking Systems. An online booking system allows ordering services and payment through web or mobile applications, allowing businesses to scale operations. Being part of the day-to-day operations, those systems require top-notch reliability, scalability, and integration with other systems. E.g Uber, Delivery Hero, Trivago, Sixt, etc
  3. Golang is used in Ecommerce. E-commerce startups, SMEs, and large enterprises all face challenges in regard to page load times, scalability issues, and availability of websites i.e. during major promotions. Golang is widely used for solving those problems. Examples are Alibaba, Allegro, Mercadolibre etc.
  4. Golang is used in Fintech. In fintech (financial technology), there is a continuous and 24/7 demand for financial data from many customers. Golang is a really good fit when it comes to handling transactions and accessing financial data. Examples are Solaris Bank, Monzo Bank, Capital One, Sterling Bank, etc.
  5. Golang is used in Payments Processing Systems. Payment processing systems allow businesses to take payments for goods and services using credit cards, debit cards, and checks. These systems check the details received during the transaction and carry out a series of anti-fraud measures before settling the transaction. With 24/7 global operations and hard SLAs, stability and automatic recovery are a must-have since each step in the payment processing pipeline requires the lowest possible latency which Golang delivers. Examples are America Express, PayPal, Bolt, iZettle, etc

Others are chats and messaging platforms, cybersecurity, IoT data processing, real-time events platforms, and gaming servers.

A Simple Calculator Built Using Golang

A Go project always has a main.go which is the entry point of the project.

package main

import "fmt"

//addition
func add(numbers []int) int {
result := 0
for _, number := range numbers {
result += number
}
return result
}
//subtraction
func subtract(numbers []int) int {
result := numbers[0]
for i := 1; i < len(numbers); i++ {
result -= numbers[i]
}
return result
}

//multiply
func multiply(numbers []int) int {
result := 1
for _, number := range numbers {
result *= number
}
return result
}

//division
func divide(numbers []int) int {
result := numbers[0]
for i := 1; i < len(numbers); i++ {
result /= numbers[i]
}
return result
}
func main() {
numbers := []int{1, 2, 3, 4}
fmt.Println("Addition:", add(numbers))
fmt.Println("Subtraction:", subtract(numbers))
fmt.Println("Multiplication:", multiply(numbers))
fmt.Println("Division:", divide(numbers))
}

Testing

Below is a unit test to verify that individual units of code (such as functions) work as expected. Unit tests help to catch errors and bugs early in the development process, before the code is deployed to production. There are other types of test such as Integration test, Performance test, Acceptance test, Regression test, etc.

package main

import (
"reflect"
"testing"
)

func TestAdd(t *testing.T) {
input := []int{1, 2, 3, 4}
expected := 10
actual := add(input)
if actual != expected {
t.Errorf("add(%v) = %d; expected %d", input, actual, expected)
}
}

func TestSubtract(t *testing.T) {
input := []int{1, 2, 3, 4}
expected := -8
actual := subtract(input)
if actual != expected {
t.Errorf("subtract(%v) = %d; expected %d", input, actual, expected)
}
}

func TestMultiply(t *testing.T) {
input := []int{1, 2, 3, 4}
expected := 24
actual := multiply(input)
if actual != expected {
t.Errorf("multiply(%v) = %d; expected %d", input, actual, expected)
}
}

func TestDivide(t *testing.T) {
input := []int{24, 2, 3, 4}
expected := 1
actual := divide(input)
if actual != expected {
t.Errorf("divide(%v) = %d; expected %d", input, actual, expected)
}
}

Conclusion

In conclusion, I will say “Why not Go”. Golang has become a popular choice for software development due to its support for concurrency, scalability, and ease of use. Its clear documentation and built-in testing package make it an attractive option for developers. Additionally, Golang is backed by major companies such as Google, which invests heavily in its development and adoption. As a result, Golang is poised to become a major player in the future of software development.

--

--

Cecilia Orji
Cecilia Orji

Written by Cecilia Orji

A Golang developer | love programming as it helps to solve problems | hobbies : listening to radio, reading and a good Kdrama

No responses yet