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

Only $11.99/month after trial. Cancel anytime.

Hands-on Go Programming: Learn Google’s Golang Programming, Data Structures, Error Handling and Concurrency ( English Edition)
Hands-on Go Programming: Learn Google’s Golang Programming, Data Structures, Error Handling and Concurrency ( English Edition)
Hands-on Go Programming: Learn Google’s Golang Programming, Data Structures, Error Handling and Concurrency ( English Edition)
Ebook381 pages4 hours

Hands-on Go Programming: Learn Google’s Golang Programming, Data Structures, Error Handling and Concurrency ( English Edition)

Rating: 5 out of 5 stars

5/5

()

Read preview

About this ebook

Hands-on Go Programming is designed to get you up and running as fast as possible with Go. You will not just learn the basics but get introduced to how to use advanced features of Golang.

The book begins with the basic concepts of Data types, Constants, Variables, Operators, Reassignment, and Redeclaration. Moving ahead, we explore and learn the use of Functions, Control flows, Arrays, Slices, Maps, and Structs using some great examples and illustrations. We then get to know about Methods in Golang. Furthermore, we learn about complex aspects of Golang such as Interfaces,Pointers, Concurrency and Error Handling.

By the end, you will be familiar with both the basics and advanced concepts of Go and start developing critical programs working using this language.
LanguageEnglish
Release dateMar 4, 2021
ISBN9789389898200
Hands-on Go Programming: Learn Google’s Golang Programming, Data Structures, Error Handling and Concurrency ( English Edition)

Related to Hands-on Go Programming

Related ebooks

Programming For You

View More

Related articles

Reviews for Hands-on Go Programming

Rating: 5 out of 5 stars
5/5

1 rating0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Hands-on Go Programming - Sachchidanand Singh

    CHAPTER 1

    Introduction

    The Go programming language was conceived by Robert Griesemer, Rob Pike, and Ken Thompson in 2007 at Google. It’s an open-source, general-purpose programming language that supports high-performance networking and multiprocessing. The Go compiler was initially written in C but it is now written in Go itself.

    Structure

    Data types

    Constants, variables, and operators

    Typed constants and untyped constants

    Multiple constant declarations

    Redeclaration concept

    Reassignment concept

    Code structure

    Objective

    This chapter covers the basic concepts of data types, constants, variables, operators, reassignment, and redeclaration. You will learn how to use them in the Go programming language.

    Introduction

    Go is a systems-level programming language for large distributed systems and highly scalable network servers. It’s well-suited for building infrastructures like networked servers and tools and is suitable for cloud, mobile applications, machine learning, etc. Go provides efficient concurrency, flexible approach to data abstraction, and supports automatic memory management, i.e., garbage collection.

    Why Go programming?

    Multithreading is supported by most of the programming languages, but race conditions and deadlocks create difficulties in creating the multithreaded application. For example, creating a new thread in Java consumes approximately 1 MB of the memory heap size. Now, consider a case wherein you need to spin thousands of such threads. Then, it will create out of memory.

    Moreover, there is a limit to the number of cores you can add to the processors like quad-core and octa-core to increase processing power. You cannot keep on adding more cache to the processor in order to boost performance since there is a cost involved. Therefore, we are left with only one option to build a more efficient software having high performance.

    Go provides a solution to the problem with goroutines. You can spin millions of them at a time since they consume ~2KB heap memory. Goroutines have faster startup time and they use more memory on a need basis only. Also, a single goroutine can run on multiple threads since goroutines and OS threads don’t have 1:1 mapping.

    1.1 Data types

    Data types categorize a set of related values and describe the operations that can be performed.

    1.1.1 Numeric types

    They are arithmetic types and represent either integer types or floating-point values.

    Integer types:

    Table 1.1

    Float types:

    Table 1.2

    1.1.2 String types

    Strings are immutable types. This means that once created, you can’t change the contents of a string. Go supports two styles of string literals: the double-quote style and the back-quote style.

    String literals can be created using double quotes, Go Programming or backticks, 'Go Programming'. With regular double-quoted strings, the special sequences like newlines are interpreted as actual newlines while escape sequences are ignored in the backtick character and treated as normal values. For example, \n gets replaced with a newline in double-quoted strings as shown below:

    Program 1.1

    //Go program showing newline sequence

    package main

    import fmt

    func main() {

    // newline sequence is treated as a special value

    x := apple\norange

    fmt.Println(x)

    // newline sequence is treated as two raw characters

    y := ‘apple\norange’

    fmt.Println(y)

    }

    The following will be the output for the above program:

    apple

    orange

    apple\norange

    1.1.3 Boolean types

    Program 1.2

    //Go program to explain boolen types

    package main

    import fmt

    func main() {

    var b bool

    fmt.Println(b)

    b = true

    fmt.Println(b)

    }

    The following will be the output for the above program:

    false

    true

    1.1.4 Derived types

    The derived type may include structure types, function types, slice types, interface types, map types, and channel types.

    1.2 Constants

    In Go, const is a keyword that introduces a name for a scalar value like 3.14159. Such values are called constants.

    Type inference: When a variable is declared without specifying an explicit type (either by using the := syntax or the var = expression syntax), the variable’s type is inferred from the value on the right-hand side.

    Constants:

    Constants are declared with the const keyword and can be a numeric, string, Boolean, or character values.

    Constants cannot be declared using the := syntax

    E.g.: const Pi = 3.14

    1.2.1 Variable declaration

    Variables declared without an explicit initial value are assigned the default zero value for numeric types, false for boolean types, and (the empty string) for string types. Let’s look at the following program:

    Program 1.3

    //Go program showing variables declared

    //without an explicit initial value

    package main

    import fmt

    func main() {

    var f float64

    var i int

    var b bool

    var s string

    fmt.Printf(%v %v %v %q\n, f, i, b, s)

    }

    The following will be the output for the above program:

    0 0 false

    In Go, the type of a variable is specified after the variable name.

    If you want to put two (or more) statements on one line, they must be separated with a semicolon (;).

    When declaring a variable, it is assigned the natural null value for the type.

    This means that here, after var i int, i has a value of 0.

    With var s string, s is assigned the zero string which is .

    Compare the following pieces of code which have the same effect:

    Declaration with =

    var a int = 20

    var b bool = true

    The var keyword is used to declare a variable and then assign a value to it.

    Declaration with :=

    You can drop var and data-type in this syntax.

    a := 20

    b := true

    When Go finds the := assignment syntax, it understands that a new variable needs to be declared with an initial value. But you cannot use this syntax to assign a value to a pre-defined variable. The variable type is deduced from the value. For example, a value of 20 indicates an int and a value of true tells Go that the type should be Boolean.

    1.2.2 Short variable declaration

    In Go, you can declare variables in the following two ways:

    Using var keyword

    Using a short declaration operator (:=)

    The short variable declaration operator (:=) is used to declare and initialize the local variables inside a function. It narrows the scope of the variable.

    Using a short declaration operator, variables are declared without specifying the type. The type of variable is determined by the type of expression on the right-hand side of the := operator.

    Syntax of short variable declaration operator:

    variable_name := expression or value

    Examples:

    a := 10

    x, y := 100, 200

    p, q, r := 150, 250, 300

    Short variable declaration:

    It is used to declare and initialize variables inside a function only.

    The variable declaration and initialization are made at the same time.

    Variables are declared inside the function, hence they have local scope.

    It is not required to mention the type of the variable in the short variable declaration.

    1.3 Operator

    Operators are used to performing the given mathematical or logical calculations. The following built-in operators are supported by Golang:

    Arithmetic operators

    Relational operators

    Logical operators

    Bitwise operators

    Assignment operators

    Miscellaneous operators

    1.3.1 Arithmetic operators

    Refer to the following table:

    Table 1.3

    1.3.2 Relational operators

    Refer to the following table:

    Enjoying the preview?
    Page 1 of 1