Learning Go Programming: Build ScalableNext-Gen Web Application using Golang (English Edition)
()
About this ebook
Related to Learning Go Programming
Related ebooks
Hands-on Go Programming: Learn Google’s Golang Programming, Data Structures, Error Handling and Concurrency ( English Edition) Rating: 5 out of 5 stars5/5Go Programming Blueprints Rating: 0 out of 5 stars0 ratingsThe Art of Go - Basics: Introduction to Programming in Go: Read and Learn Programming Rating: 0 out of 5 stars0 ratingsGo Cookbook Rating: 5 out of 5 stars5/5Data Structures and Algorithms with Go: Create efficient solutions and optimize your Go coding skills (English Edition) Rating: 0 out of 5 stars0 ratingsGo Programming Blueprints - Second Edition Rating: 4 out of 5 stars4/5Get Programming with Go Rating: 5 out of 5 stars5/5Mastering Go: Navigating the World of Concurrent Programming Rating: 0 out of 5 stars0 ratingsLearn Rust Programming: Safe Code, Supports Low Level and Embedded Systems Programming with a Strong Ecosystem (English Edition) Rating: 0 out of 5 stars0 ratingsMastering Go Programming: From Basics to Expert Proficiency Rating: 0 out of 5 stars0 ratingsGolang for Jobseekers: Unleash the power of Go programming for career advancement (English Edition) Rating: 0 out of 5 stars0 ratingsGolang Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #1 Rating: 0 out of 5 stars0 ratingsLearning JavaScript Data Structures and Algorithms Rating: 5 out of 5 stars5/5Java 9 Data Structures and Algorithms Rating: 0 out of 5 stars0 ratingsBuilding Server-side and Microservices with Go: Building Modern Backends and Microservices Using Go, Docker and Kubernetes Rating: 0 out of 5 stars0 ratingsThe Go Workshop: Learn to write clean, efficient code and build high-performance applications with Go Rating: 0 out of 5 stars0 ratingsThe Way to Go: A Thorough Introduction to the Go Programming Language Rating: 3 out of 5 stars3/5Go Design Patterns Rating: 5 out of 5 stars5/5Learning Go Programming Rating: 5 out of 5 stars5/5Learn Kubernetes & Docker - .NET Core, Java, Node.JS, PHP or Python Rating: 0 out of 5 stars0 ratingsModern Web Development with Go: Build real-world, fast, efficient and scalable web server apps using Go programming language Rating: 0 out of 5 stars0 ratingsDesigning Microservices using Django: Structuring, Deploying and Managing the Microservices Architecture with Django Rating: 0 out of 5 stars0 ratingsInfrastructure as Code (IAC) Cookbook Rating: 0 out of 5 stars0 ratingsMastering Kubernetes Rating: 5 out of 5 stars5/5Getting Started with Terraform Rating: 5 out of 5 stars5/5
Trending on #Booktok
A Court of Mist and Fury Rating: 5 out of 5 stars5/5Icebreaker: A Novel Rating: 4 out of 5 stars4/5It Ends with Us: A Novel Rating: 4 out of 5 stars4/5The Assassin and the Pirate Lord: A Throne of Glass Novella Rating: 4 out of 5 stars4/5The Secret History: A Read with Jenna Pick: A Novel Rating: 4 out of 5 stars4/5Powerless Rating: 4 out of 5 stars4/5A Little Life: A Novel Rating: 4 out of 5 stars4/5Pride and Prejudice Rating: 4 out of 5 stars4/5Normal People: A Novel Rating: 4 out of 5 stars4/5The Love Hypothesis Rating: 4 out of 5 stars4/5If We Were Villains: A Novel Rating: 4 out of 5 stars4/5The Summer I Turned Pretty Rating: 4 out of 5 stars4/5Funny Story Rating: 4 out of 5 stars4/5Happy Place Rating: 4 out of 5 stars4/5Once Upon a Broken Heart Rating: 4 out of 5 stars4/5Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones Rating: 4 out of 5 stars4/5Seven Stones to Stand or Fall: A Collection of Outlander Fiction Rating: 4 out of 5 stars4/5Better Than the Movies Rating: 4 out of 5 stars4/5Fire & Blood: 300 Years Before A Game of Thrones Rating: 4 out of 5 stars4/5The 48 Laws of Power Rating: 4 out of 5 stars4/5Crime and Punishment Rating: 4 out of 5 stars4/5Beauty and the Beast Rating: 4 out of 5 stars4/5Dune Rating: 4 out of 5 stars4/5Divine Rivals: A Novel Rating: 4 out of 5 stars4/5Rich Dad Poor Dad Rating: 4 out of 5 stars4/5The Lord Of The Rings: One Volume Rating: 5 out of 5 stars5/5The Little Prince: New Translation Version Rating: 5 out of 5 stars5/5Finnegans Wake Rating: 4 out of 5 stars4/5Beach Read Rating: 4 out of 5 stars4/5Milk and Honey: 10th Anniversary Collector's Edition Rating: 4 out of 5 stars4/5
Reviews for Learning Go Programming
0 ratings0 reviews
Book preview
Learning Go Programming - Shubhangi Agarwal
CHAPTER 1
Introduction to Go
Programming languages enable us to communicate with computers by developing various types of programs and applications. We have multiple programming languages in our technical world such as C, C++, PHP, Python, Ruby, and many others. Go language is one of those growing programming languages that solves many issues that we get from the other languages. Let’s go through the evolution of the Go language, its features, scope, and use in the technical field.
Structure
In this chapter, we will discuss the following topics:
Software essence
Evolution of Go language and its success
Characteristics of Go language
Object-oriented nature of Go language
The analogy with other languages
Pain points of Go language
Application of Go language
Objective
After studying this chapter, you’ll be able to understand the:
Evaluation of Go language
Need for Go language
Characteristics of Go language
How it is different from other programming languages
Applications developed in Go language
A look at software essence
At the time of Go’s initiation, complexity was the major concern in software development. Usually, development used to be done by C++ or Java but there were some challenges with them like security, memory utilization, speed, efficiency, debugging, etc. Software should have the power to function correctly against potential risks produced by malicious attacks. Dead objects shouldn’t exist to avoid resource blocking and improve the efficiency of software.
Evolution of Golang and its success
By looking at the problems with software development, it was decided that with a rise of networked and multicore computing, a language must support some sort of concurrency and parallelism, garbage collection, and should automate memory management. All that reflected into a thread of discussion, a bunch of ideas, decent strategy, and finally a great development. With Golang, efficient compilation, execution, and ease of programming are all possible with one language.
Go language is sponsored by Google and created by Robert Griesemer, Rob Pike, and Ken Thompson have depicted the goals of Golang. It finally came out in public on November 10, 2009. It is an open-source language. Go language’s creators and the community have built many tools which are natively supported by the language core, that in most cases don’t need any third-party library. For example, Go language has HTTP, JSON, HTML template, built-in language natively, and it can build very complex API services without even thinking about finding a library on GitHub. Now, millions of programmers are working on Go and many more are to come.
Figure 1.1: Logo change in April 2018
Characteristics of Go
Go is a statically and fast-compiled language that is designed to be used with multicore architecture for efficient memory utilization. Here are some features of Golang:
Automatic garbage collection
With this feature, the programmer does not need to worry about how the memory will be managed all around. Currently, mark-and-sweep
collector is used in Go. It keeps track of all the created objects. Whenever necessary, it first pauses the running process, works on garbage objects, and then frees the process to run again. It also supports concurrency.
Concurrency
To run more than one task simultaneously is called concurrency. Golang provides a different approach to execute multiple functions at the same time. That is possible with Goroutines and channels. In Go, Goroutines can be considered as the functions that can be executed parallelly with other functions. On the other hand, the channel provides functionality to communicate between multiple Goroutines.
Static
Golang is built as a compiled language, and Google developers did a great job with it. Using static linking, it combines all dependent libraries and modules into one single binary file based on OS type and architecture which means that while compiling the backend application, just upload the compiled binary into the server and it will work without installing any dependencies there.
Ease testing
Go language offers a rich library that supports automated testing. Here is a package called testing that helps to run the go test command which makes the execution of any testing function possible. There are some benchmarks. There are many forms of testing functions that are executed by go test commands.
Standard library
The important part of good programming is code reusability. The standard library of Go language contains a huge number of packages that provide great functionality for Go applications. It provides packages like OS, runtime, HTTP, XML, and JSON that connect with operating systems and give support for runtime.
Cross-platform
Go is a cross-platform language that can run on Linux, Mac, Free BSD, Windows, iOS, Android, and others.
Documentation
Golang has a command Godoc that concentrates on package documentation and serves it as HTML or plain text. With the help of this documentation, the programmer can steer from illustration to the implementation of any function. It defines a package’s functions, constants, variables, and even the type that Go supports.
Object-oriented nature
A standard definition of object-oriented language doesn’t work with Go but it provides the same functionality in some other manner. There is a defined data structure struct that integrates fields and methods. Golang has no inheritance but it has a different approach to establish the relation between structs. Object composition establishes a IS-A relationship, whereas interfaces are used to form Has-A relation.
Furthermore, methods in the Go language are more transparent. They are not bound with structs. Go doesn’t support the feature of overloading. In this way, Go language makes things simpler and more convenient.
The analogy with other languages
In C++, Java, and other object-oriented languages, inheritance, overloading, and generic types are the key features. Here, the Go team focuses on concurrency, garbage collection, and fast compilation to work well with parallel systems. Let’s go through some nuts and bolts of the programming language:
Type-safe
This refers to a feature to prevent type
errors. Just like C, C++, Java, and Python, Go is also a type-safe language. The Go compiler meets a certain level of validity at the time of compilation. It ensures that if there is +
operator, then both the operands are of numeric type.
Object-oriented
Go can be considered as a light-weight object-oriented language. However, it supports types and methods that can be defined for any sort of data but have no classes and inheritance. Not everything is an object associated here. Go syntax resembles the C language. It is statically typed and scalable to larger systems like C++ and Java.
Inheritance
Go is not an object-oriented language. It doesn’t support inheritance but it has a feature of composition and interfaces. With composition, it is possible to embed more than two structs together. Multiple methods can be added in interface, and thus, it has a chance to grow. This new concept is more powerful than the class-based inheritance.
Overloading
Go doesn’t have a concept of method and operator overloading unlike C++ and Java. Identifying methods by only their name removes complexity and makes it simple to code.
Exception handling
Unlike C++ and Java, there are no try-catch-finally idioms in Go. It has some built-in functions to signal and recover the exceptional conditions. Go functions return the error as the last value. Here is a simple mechanism: the last value is compared to nil.
If it is, there is no error; otherwise, there would be an error.
Concurrency
In Java, there is an option of threads for concurrent programming. Java has a lot of utilities to manage those threads but still, it is considered quite expensive and hard to understand. To make it simple, Go has a simple technique for concurrency with the feature of Go routines and channels. Go routines are light-weighted threads, and the channel helps to communicate between Go routines.
Garbage collection
Generally, there are two ways of memory management. One is automatic reference counting and the other one is a garbage collection. Like C# and Java, Go also has a garbage collector. With Objective-C, Swift uses the reference counting technique to manage the resources. Current GC mark-and-sweep
is used with Go language. It runs on a separate CPU, parallel with main programs, and thus, it helps with multicore architectures.
Pain points of Go language
With multiple features, Golang has some disadvantages. The limitations of Go language are as follows:
Generic Programming
Go doesn’t have compile-time generics that lead duplicate code or typecast as per requirement. However, there are first-class functions but we can’t reuse the same functions with a different type of collection. It somehow affects efficiency and productivity as well.
Tedious error handling
Error handling is not perfect in the Go language. There is no execution carried out from the compiler to handle the error. It depends on the programmer as to how the functions can be used to return the error whenever it occurs.
Applications of Golang
Go has been gaining popularity since it was first announced in 2009. A few companies are working on the Go language such as IBM, Twitter, Facebook, YouTube, Apple, BBC, Google, and many others. No one can predict the future, but Golang has multiple reasons to grow.
Here are some applications that are developed in the Go language.
Docker
It is a set of tools to create, deploy, and run the distributed application.
It is an open-source platform that allows the extending of its features if needed.
OpenShift
An open-source cloud computing platform provided as a service by Red Hat.
It provides the service for container-based software deployment and management.
Kubernetes
It is an extensible open-source container cluster management system that promotes seamlessly automated deployment processes.
Dropbox
Dropbox migrated from Python to Go language to scale its system in a more efficient manner and that supports better concurrency and fast execution.
InfluxDB
It is an open-source time-series database that fetches time series data.
It supports the Internet of Things sensor data, application matrices, and real-time analytics.
Conclusion
In this chapter, you learned the pain points of software development and how the evolution of the Go language resolved them. This open-source programming language makes software development easy, efficient, and reliable. We saw some applications of various organizations developed with Go language. We discussed some disadvantages of Go language that we need to remember while programming in Golang. This is all the theoretical knowledge we need to know. Let’s go ahead with the installation of Go language in the next chapter.
Questions
Who developed the Go language?
How does garbage collection work in Go language?
Which command illustrates Go language documentation?
In what ways is Go language different from other programming languages?
How are fields and methods integrated into Golang?
Go is a concurrent language.
What does that mean?
CHAPTER 2
Environment Setup
After becoming aware about the Go language, let’s move on to the Golang system requirements and installation process. This chapter defines the system requirements to install Golang, text editors to write Golang programs, and all about the installation and uninstallation process with various operating systems. After installing Go, the first Golang program to check the proper installation is illustrated.
Structure
In this chapter, we will discuss the following topics:
System requirements for Go language
Suitable text editors
How to install/uninstall Go on your system
How to write the first test program on Go
Objective
After studying this chapter, you’ll be able to:
Get the suitable text editor for Go language
Install Golang on your system
Write a test program on any text editor
Run a simple Go program
Uninstall Go if there are any issues
System requirements
Go targets multiple platforms like Linux, Mac, FreeBSD, Windows of 32-bit, and 64-bit x86 processor architectures. In case of any other operating system, one can use gccgo, the compiler of Go language.
Go1.15 is the latest version of Go and was released in 2020. First, it required OpenBSD 6.2, then later, Mac OS 10.10 Yosemite, and then, Windows 7. Supports for previous versions of these operating systems have been removed.
To run a simple Go program, there is a need for text editor and Go compiler. These could be as follows:
Text editors
Go supports a variety of editor plugins and IDEs to make Go development more productive and seamless. There are most of the well-known text editors that either directly facilitate or through some plugins. Text editors can vary with different operating systems.
Here are some text editors:
Atom
Atom is a free and open-source text editor for Windows, Linux, and macOS.
It is a cross-platform java-script oriented desktop application from GitHub.
BBedit
BBedit is a commercial text editor developed for macOS.
Go-bbpackage comes with clippings, ctags standard library completion, better syntax highlighting, and tools.
Eclipse IDE
Eclipse IDE is the most widely used open-source cross-platform IDE.
GoClipse is an Eclipse extension that adds IDE functionality to Go language.
Emacs
Emacs is free and loaded by multiple extensible and customizable text editors.
Code auto-completion, code analysis and refactoring, error checking, playground and code snippets are additional features of Emacs.
Gedit
Gedit is a default text editor for the GNOME desktop.
Go-gedit-plugin is a Go programming plugin for gedit editor.
Goland
Goland is a commercial cross-platform IDE by JetBrains.
The IDE analyzes the code, connection between symbols, quick navigation, error analysis, formatting and refactoring.
Notepad++
Notepad++ is a distributed source code text editor for Microsoft Windows.
GOnpp plugin provides code completion, function call tips, Go imports integration, and keyboard shortcuts for common Go commands.
Sublime Text
Sublime Text is a free cross-platform source code editor that supports auto-save, customizable key binding, macros, and many others.
GoSublime is a Golang plugin collection that provides the feature of code completion from Go code, automatically adding/removing package imports, etc.
Vim
Vim (VI improved) is a free open-source text editor for Unix.
It is designed to use both from a command-line interface and as a standalone application in a graphical user interface.
Visual Studio
Visual Studio is a commercial IDE for Windows to develop websites, web apps, web services, and mobile apps.
With Visual Studio, the Go extension supports for multiple features like IntelliSense code auto-completion, hover information, signature help, snippet, etc.
Cloud-based IDEs
Development of cloud IDEs is a great innovation in the software industry. In such IDEs, the developer’s atomic services like syntax analysis, build, run, and debug are operated on separate horizontally-scalable microservices. These are some cloud IDEs:
Cloud9
Cloud9 is an online open-source multi-language cloud-based IDE.
It connects with GitHub, Bitbucket, and Google cloud platform repositories.
It has MySQL and other databases; and terminal and immediate windows.
CodeEnv
CodeEnv is also an open-source multi-language cloud-based IDE.
It is a browser editor that has a feature to edit, build, run, and debug the project.
Wide
Wide is a web-based IDE for Go language.
It uses a code mirror-based themed programming editor with Gocode-assisted syntax highlighting, auto-completion, and real-time collaboration.
Micro
Micro is a toolkit for cloud-native development.
It provides an opinionated framework for developing applications with a pluggable architecture.
Compiler
There are several compilers running for Go language that help to set up the Go environment. Two major implementations of Go compiler areas:
Gc
Gc is a default compiler with a recursive descent parser and it uses a custom loader. It supports popular processors like x86 (32/64bit) and ARM.
Gccgo
Gccgo is a part of GCC, the GNU compiler collection that supports the front-end. It supports all the processors that GCC supports. Gccgo is slower than Gc but supports more processors and has more powerful optimization and so, a CPU-bound program built by gccgo will run fast.
A third Go compiler also exists which is known as GopherJS. It compiles Go code into java-script code and enables Go to be used for front-end development that runs in all browsers.
How to install
The following procedures prepares an environment to install Go on various platforms.
Windows installation
Here are the steps to install Go on the Windows machine.
One can visit the Go page on golang.org and download Go.
Figure 2.1: ‘golang.org’ home page
Download MSI or zip file as per the system requirement.
Figure 2.2: Go for multiple OS
Various versions of Golang are listed below:
Figure 2.3: Select as per your system configuration
Follow the prompts: Run |Next
Figure 2.4: Run file
Here are some license agreements that need to be accepted to install Golang.
Figure 2.5: Accept License