Hands-on Go Programming: Learn Google’s Golang Programming, Data Structures, Error Handling and Concurrency ( English Edition)
5/5
()
About this ebook
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.
Related to Hands-on Go Programming
Related ebooks
Building Server-side and Microservices with Go: Building Modern Backends and Microservices Using Go, Docker and Kubernetes Rating: 0 out of 5 stars0 ratingsGo Programming Blueprints Rating: 0 out of 5 stars0 ratingsLearning Go Programming: Build ScalableNext-Gen Web Application using Golang (English Edition) Rating: 0 out of 5 stars0 ratingsThe Way to Go: A Thorough Introduction to the Go Programming Language Rating: 2 out of 5 stars2/5Designing Microservices using Django: Structuring, Deploying and Managing the Microservices Architecture with Django Rating: 0 out of 5 stars0 ratingsGo Programming Cookbook: Over 75+ recipes to program microservices, networking, database and APIs using Golang Rating: 0 out of 5 stars0 ratingsAndroid application development with Kotlin: Build Your First Android App In No Time Rating: 0 out of 5 stars0 ratingsClojure for Java Developers Rating: 0 out of 5 stars0 ratingsThe Black Book of the Programmer Rating: 0 out of 5 stars0 ratingsReactive State for Angular with NgRx 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 ratingsGo Programming Blueprints - Second Edition Rating: 5 out of 5 stars5/5Go in Practice Rating: 5 out of 5 stars5/5Learning Go Programming Rating: 5 out of 5 stars5/5Go in Action Rating: 5 out of 5 stars5/5Get Programming with Go Rating: 0 out of 5 stars0 ratingsGo Web Programming Rating: 5 out of 5 stars5/5Tanmay Teaches Go: The Ideal Language for Backend Developers Rating: 0 out of 5 stars0 ratingsScala Functional Programming Patterns Rating: 0 out of 5 stars0 ratingsGo Design Patterns Rating: 5 out of 5 stars5/5Modern Web Development with Go: Build real-world, fast, efficient and scalable web server apps using Go programming language Rating: 0 out of 5 stars0 ratingsGo Cookbook Rating: 5 out of 5 stars5/5Functional Reactive Programming Rating: 0 out of 5 stars0 ratingsHands-On Microservices with Kubernetes: Build, deploy, and manage scalable microservices on Kubernetes Rating: 5 out of 5 stars5/5Mastering Akka Rating: 0 out of 5 stars0 ratingsThe Little Elixir & OTP Guidebook Rating: 0 out of 5 stars0 ratings
Programming For You
Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5HTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5Java for Beginners: A Crash Course to Learn Java Programming in 1 Week Rating: 5 out of 5 stars5/5SQL: For Beginners: Your Guide To Easily Learn SQL Programming in 7 Days Rating: 5 out of 5 stars5/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Python Machine Learning By Example Rating: 4 out of 5 stars4/5101 Amazing Nintendo NES Facts: Includes facts about the Famicom Rating: 4 out of 5 stars4/5Pokemon Go: Guide + 20 Tips and Tricks You Must Read Hints, Tricks, Tips, Secrets, Android, iOS Rating: 5 out of 5 stars5/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Learn SQL in 24 Hours Rating: 5 out of 5 stars5/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5Modern C++ for Absolute Beginners: A Friendly Introduction to C++ Programming Language and C++11 to C++20 Standards Rating: 0 out of 5 stars0 ratingsPython Projects for Beginners: A Ten-Week Bootcamp Approach to Python Programming Rating: 0 out of 5 stars0 ratings
Reviews for Hands-on Go Programming
1 rating0 reviews
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: