Hands-on Go Programming: Learn Google’s Golang Programming, Data Structures, Error Handling and Concurrency ( English Edition)
5/5
()
Maps
Data Types
Go Programming Language
Pointers
Structs
Error Handling
Go Programming
Interfaces
Slices
Arrays
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
Learning 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: 3 out of 5 stars3/5The Art of Go - Basics: Introduction to Programming in Go: Read and Learn Programming 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 Cookbook Rating: 5 out of 5 stars5/5Go Programming Blueprints - Second Edition Rating: 4 out of 5 stars4/5Go Programming Blueprints Rating: 0 out of 5 stars0 ratingsGet Programming with Go 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 ratingsMastering Go Programming: From Basics to Expert Proficiency 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 ratingsMastering Go: Navigating the World of Concurrent Programming Rating: 0 out of 5 stars0 ratingsGo Programming Cookbook Rating: 0 out of 5 stars0 ratingsJava 9 Data Structures and Algorithms 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 ratingsMastering Rust Programming: From Foundations to Future Rating: 0 out of 5 stars0 ratingsData Structures and Algorithms with Go: Create efficient solutions and optimize your Go coding skills (English Edition) Rating: 0 out of 5 stars0 ratingsRust Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #5 Rating: 0 out of 5 stars0 ratingsLearning Go Programming Rating: 5 out of 5 stars5/5Building Server-side and Microservices with Go: Building Modern Backends and Microservices Using Go, Docker and Kubernetes 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 ratingsGo Design Patterns Rating: 5 out of 5 stars5/5Rust In Practice Rating: 0 out of 5 stars0 ratingsHow To Build Microservices: Top 10 Hacks To Modeling, Integrating & Deploying Microservices Rating: 0 out of 5 stars0 ratingsInfrastructure as Code (IAC) Cookbook Rating: 0 out of 5 stars0 ratingsLearn Docker - .NET Core, Java, Node.JS, PHP or Python: Learn Collection Rating: 5 out of 5 stars5/5
Programming For You
Coding All-in-One For Dummies Rating: 4 out of 5 stars4/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/5Spies, Lies, and Algorithms: The History and Future of American Intelligence Rating: 4 out of 5 stars4/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5Excel 101: A Beginner's & Intermediate's Guide for Mastering the Quintessence of Microsoft Excel (2010-2019 & 365) in no time! Rating: 0 out of 5 stars0 ratingsPython: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5Photoshop For Beginners: Learn Adobe Photoshop cs5 Basics With Tutorials Rating: 0 out of 5 stars0 ratingsPython Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5iPhone For Dummies Rating: 0 out of 5 stars0 ratingsA Slackers Guide to Coding with Python: Ultimate Beginners Guide to Learning Python Quick Rating: 0 out of 5 stars0 ratingsJavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5Teach Yourself C++ Rating: 4 out of 5 stars4/5HTML in 30 Pages Rating: 5 out of 5 stars5/5Windows 11 For Dummies Rating: 0 out of 5 stars0 ratingsPython Data Structures and Algorithms Rating: 5 out of 5 stars5/5Learn SQL in 24 Hours Rating: 5 out of 5 stars5/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5HTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5
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: