Discover millions of audiobooks, ebooks, and so much more with a free trial

From $11.99/month after trial. Cancel anytime.

Learning Go Programming: Build ScalableNext-Gen Web Application using Golang (English Edition)
Learning Go Programming: Build ScalableNext-Gen Web Application using Golang (English Edition)
Learning Go Programming: Build ScalableNext-Gen Web Application using Golang (English Edition)
Ebook540 pages4 hours

Learning Go Programming: Build ScalableNext-Gen Web Application using Golang (English Edition)

Rating: 0 out of 5 stars

()

Read preview

About this ebook

This book is a unique read for both beginners and developers as it extensively covers topics ranging from fundamentals to advanced topics in Go programming. Basics such as Data types, Control structures and Loops in have been explained in-depth. A detailed description of Structs, Interfaces, Polymorphism and Concurrency will enable you to write professional codes using Golang. You will get an idea of error data type and how to recover it in Golang. You will be capable of using standard libraries, create custom packages and install third party packages in Go. Creation of functions and invoking them in Go have been vividly explained. By the end, you will be able to write advanced Golang code and at the same time, develop an application with Golang server.
LanguageEnglish
PublisherBPB Online LLP
Release dateJan 2, 2021
ISBN9789389898439
Learning Go Programming: Build ScalableNext-Gen Web Application using Golang (English Edition)

Related to Learning Go Programming

Related ebooks

Trending on #Booktok

Reviews for Learning Go Programming

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    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

    Enjoying the preview?
    Page 1 of 1