Introduction to Google's Go Programming Language: GoLang
By Orhan Gazi
()
About this ebook
This book teaches go programming language. Go was originally designed at Google in 2007.
Go is a fast and lightweight programming language. It has a quicker compilation time compared to C/C++.
Go has automatic garbage collector that frees up memory when it is no longer needed.
Go is a statically typed language, that is, errors can be caught at compile time rather than at runtime.
Go was designed to write programs for networking, and cloud-based or server-side applications.
Go has cross-platform support property, it can be compiled to run on many platforms, like windows, linux, mac and raspberry pi, etc.
The book is neatly written, and includes sufficient number of examples.
Author of the book uses his years of teaching experience to serve the topics of go programming in a clean and understandable manner.
Read more from Orhan Gazi
Modern C++ Programming Rating: 0 out of 5 stars0 ratingsModern C++ Programming: Including the recent standards C++11, C++17, C++20, C++23 Rating: 0 out of 5 stars0 ratingsA Friendly Introduction to MATLAB Programming Rating: 0 out of 5 stars0 ratingsBefore It is Late Rating: 0 out of 5 stars0 ratings
Related to Introduction to Google's Go Programming Language
Related ebooks
Programming Backend with Go Rating: 0 out of 5 stars0 ratingsGo Programming Cookbook Rating: 0 out of 5 stars0 ratingsHands-On Software Engineering with Golang: Move beyond basic programming to design and build reliable software with clean code Rating: 0 out of 5 stars0 ratingsMastering Concurrent 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 ratingsGo Programming Cookbook: Over 75+ recipes to program microservices, networking, database and APIs using Golang Rating: 0 out of 5 stars0 ratingsMastering Go: Navigating the World of Concurrent Programming 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/5Schematron: A language for validating XML Rating: 0 out of 5 stars0 ratingsRust In Practice Rating: 0 out of 5 stars0 ratingsRust In Practice: A Programmers Guide to Build Rust Programs, Test Applications and Create Cargo Packages 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 ratingsMastering Go Programming: From Basics to Expert Proficiency Rating: 0 out of 5 stars0 ratingsMastering Python Programming: From Basics to Expert Proficiency Rating: 0 out of 5 stars0 ratingsMastering C: A Comprehensive Guide to Programming Excellence Rating: 0 out of 5 stars0 ratingsBeyond Effective Go: Part 1 - Achieving High-Performance Code Rating: 0 out of 5 stars0 ratingsProgramming Concepts in C++ Rating: 0 out of 5 stars0 ratingsGolang Rating: 0 out of 5 stars0 ratingsLearning Rust Rating: 0 out of 5 stars0 ratingsScientific Computing with Scala Rating: 0 out of 5 stars0 ratingsCoding In C Decoded: Decoded, #1 Rating: 0 out of 5 stars0 ratingsMastering Clojure: From Basics to Expert Proficiency Rating: 0 out of 5 stars0 ratingsUltimate Nuxt.js for Full-Stack Web Applications Rating: 0 out of 5 stars0 ratingsC++ Cookbook: How to write great code with the latest C++ releases (English Edition) Rating: 0 out of 5 stars0 ratings
Computers For You
Creating Online Courses with ChatGPT | A Step-by-Step Guide with Prompt Templates Rating: 4 out of 5 stars4/5Data Analytics for Beginners: Introduction to Data Analytics Rating: 4 out of 5 stars4/5Mastering ChatGPT: 21 Prompts Templates for Effortless Writing Rating: 4 out of 5 stars4/5Algorithms to Live By: The Computer Science of Human Decisions Rating: 4 out of 5 stars4/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5The Alignment Problem: How Can Machines Learn Human Values? Rating: 4 out of 5 stars4/5Storytelling with Data: Let's Practice! Rating: 4 out of 5 stars4/5Narrative Design for Indies: Getting Started Rating: 4 out of 5 stars4/5Python for Finance Cookbook: Over 50 recipes for applying modern Python libraries to financial data analysis Rating: 0 out of 5 stars0 ratingsGet Into UX: A foolproof guide to getting your first user experience job Rating: 4 out of 5 stars4/5Python for Beginners: A Crash Course to Learn Python Programming in 1 Week Rating: 0 out of 5 stars0 ratingsHow Do I Do That In InDesign? Rating: 5 out of 5 stars5/5The Unaccountability Machine: Why Big Systems Make Terrible Decisions - and How The World Lost its Mind Rating: 0 out of 5 stars0 ratingsPython Machine Learning By Example Rating: 4 out of 5 stars4/5ITIL® 4 Essentials: Your essential guide for the ITIL 4 Foundation exam and beyond, second edition Rating: 5 out of 5 stars5/5Black Holes: The Key to Understanding the Universe Rating: 5 out of 5 stars5/5Advances in Financial Machine Learning 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 Algorithmic Trading: Build and deploy algorithmic trading systems and strategies using Python and advanced data analysis Rating: 0 out of 5 stars0 ratingsDeep Learning with PyTorch Rating: 5 out of 5 stars5/5Django Building Dynamic Website With Django : A Complete Step By Step Guide To Learn to Build Modern Web Application with a Python Rating: 0 out of 5 stars0 ratingsArtificial Intelligence: The Complete Beginner’s Guide to the Future of A.I. Rating: 4 out of 5 stars4/5Learn SAP MM in 24 Hours Rating: 0 out of 5 stars0 ratingsThe ChatGPT Millionaire Handbook: Make Money Online With the Power of AI Technology Rating: 4 out of 5 stars4/5
Reviews for Introduction to Google's Go Programming Language
0 ratings0 reviews
Book preview
Introduction to Google's Go Programming Language - Orhan Gazi
Preface
In this book we explain Go programming language. Go was originally designed at Google in 2007. After its introduction, Go quickly gained popularity among programming languages. It is fast and lightweight programming language. Go is simple to learn and easy to read by other developers. It has a quicker compilation time. Go has automatic garbage collector that frees up memory when it is no longer needed. This feature eliminates the need for manual memory management, and it eliminates memory leak problem that can arise from manual memory management.
Go is a statically typed language, that is, errors can be caught at compile time rather than at runtime.
Go supports parallel programming and high-performance networking and multiprocessing applications can be achieved using Go programs. Concurrency can be achieved using go-routines and channels. Go-routines can be considered as light version of threads used in C programming. Using go-routines multiple operations can be performed at the same time. This makes Go an ideal programming language for developing high-performance and scalable network applications, as well as for performing complex computational scientific works.
Go was designed to write programs for networking, and cloud-based or server-side applications. Go has cross-platform support property, it can be compiled to run on many platforms, like windows, linux, mac and raspberry pi, etc. Go is used by many well-known companies, including Google, Uber, and Dropbox.
Go is an open-source programming language, and it is often referred to as Golang because of its former domain name, golang.org. Go has some advantages over C. C language uses stack memory to store the local variables, whereas, Go uses heap memory for local variables, and this eliminates some coding problems. For instance, when a function is terminated, local variables are destroyed in C programming, and if the function returns the address of a local variable, this creates a problem in C programming, whereas in Go programming this issue is eliminated by storing the local variables in heap memory. Syntax of Go is easier than many of the programming languages. Go has automatic data type detection feature for variables, and even pointers can be defined without specifying the pointed data type. For instance, a variable can be defined and initialized as
v := 30.7
and a pointer pointing to this variable can be defined as
ptr := &v
and variable data type and pointed data type is determined automatically by the compiler. It is clear that pointer declaration is quite easy in Go.
In this book we teach fundamental concepts of Go language. In chapter-1, data types are explained. Basic data types for integers are int, int8, int16, int32, int64 and their unsigned counterparts. Go has two data types for floating point numbers and these are float32 and float64. It is possible to define complex numbers in Go using the data types complex64 and complex128. There is only explicit type conversion available in go programming.
In chapter-2, we explain operators used in Go languages. Chapter-3 explains conditional statements. In chapter-4 functions, methods and interfaces are explained. Go functions are very powerful functions compared to the functions of other programming languages. They can return a multiple of values of different types, and due to the heap memory storage of the local variables in functions, addresses of local variables can also be returned by Go functions. Go methods are usually associated with structure data variables. Interface is a data types in Go, and this data type is a unique feature of Go language. Using interface data type and methods it is possible to write programs employing object oriented programming features.
In chapter-5 we cover structures. Accessing to the structure object elements using pointers in Go is easier in notation compared to other programming languages. Loops and arrays are covered in chapters-6 and 7. Slices are explained in chapter-8. Manipulation of arrays and slices are quite simple in Go language. Chapter-9 explains the maps in Go. Pointers are explained in details in chapter-10. Go-routines, mutex functions, channels, atomic operations which are used for concurrent program writing are covered in chapter-11. Finally, we explain file operations in chapter-12.
This book is an introductory book in Go programming. We tried to explain fundamental concepts of Go language using simple and neat examples. This book can be ready by anyone interested in computer programming and it can also be used as a text or reference book for one semester course in computer programming in colleges and universities. Finally as a last word, I dedicate this book to my younger brother İlhan Gazi who belongs to the category of good people.
Contents
Preface
Chapter-1
Data Types in Go Language
1.1 How to Write a Go Program
1.2 Go Comments
1.3 Go Variables
1.4 Formatted Print Function: Printf
1.5 Data Types in Go Programming
1.5.1 Numeric Data Types
1.5.2 Sizeof Function
1.5.3 Float Data Types
1.5.4 Formats for Print and Scan Functions
1.5.6 String Formats
1.5.7 Complex Data Types
1.5.8 Real and Imaginary Parts of a Complex Number
1.5.9 Strings
1.6 Multiple Variable Declarations
1.7 Scanf Function
1.8 Scan and Scanln Functions
1.9 Constants in Go Programming
1.10 Go Keywords
1.11 Type Conversion in Go
1.12 Global and Local Variables
Problems
Chapter-2
Operators in Go Language
2.1 Go Operators
2.1.1 Assignment Operators
2.1.2 Arithmetic Operators
2.1.3 Logical Operators
2.1.4 Bitwise Operators in Go Programming
2.1.5 Comparison Operators
Problems
Chapter-3
Conditional Statements
3.1 If Statement
3.2 If-else Statement
3.3 Conditional Ladder Structure (if else if ladder)
3.4 Multi Conditional Structures
3.5 Nested if-else Statement
3.6 Ternary Operator (?:) in Go
3.7 Switch Statement in Go
3.7.1 The fallthrough Keyword
3.7.2 The Switch Statement with Variable Initializer
3.7.3 The Switch Statement without Switch Expression
3.7.4 Type Switches in Go
Problems
Chapter-4
Functions, Methods and Interfaces in Go Programming
4.1 Functions
4.2 Functions with Input Parameters
4.3 Go Functions with Return Values
4.4 Unused Returned Function Values
4.5 Anonymous Go Functions
4.5.1 Declaring an Anonymous Function
4.5.2 Immediate Invocation of Anonymous Functions
4.5.3 Immediate Invocation of Anonymous Functions with Arguments
4.5.4 Assigning Anonymous Function to a Variable
4.5.5 Passing Anonymous Functions as Arguments
4.5.6 Variadic Functions in Golang
4.6 Methods in Go Programming
4.6.1 Receiver Types
4.6.2 Method with User Defined Data Types Other than Structs
4.7 Interfaces in Go
4.7.1 Interfaces with Ordinary Data Types
4.7.2 Type Assertions for Interface
Problems
Chapter-5
Go Structures
5.1 Structures
5.2 Structure Declaration Using the new Keyword
5.4 Types of Structs in Go
5.4.1 Named Structs
5.4.2 Anonymous Structs
5.4.3 Anonymous Fields in a Struct
5.6 Comparing and Assigning Struct Variables
Chapter-6
Go Loops
6.1 For-Loop
6.1.1 The Infinite for-loop
6.1.2 Go Range
6.1.3 The Conditional for-loop
6.1.4 The for-loop with Map
6.1.5 The Nested-for loop
6.1.6 The Continue Statement
6.1.7 Strings with For-Loop
6.1.8 The Break Statement in For-Loop
6.1.9 Do-While Loop Implementation with For-Loop
6.1.10 Goto Statement in For-Loop
6.1.11 For Channel
Problems
Chapter-7
Arrays in Go
7.1 Arrays
7.1.1 Syntaxes for Array Declaration
7.2 Accessing Array Elements
7.3 Array Initialization
7.4 Initialization of Specific Array Elements
7.5 Length of an Array
7.6 Multi-Dimensional Arrays
7.7 Copying Arrays
7.8 Passing Arrays to Functions
Problems
Chapter-8
Slices in Go
8.1 Slices
8.1.1 Zero Valued Slices
8.1.2 Length and Capacity Explanations
8.1.3 Appending Slices
8.1.4 Concatenating the Slices
8.1.5 Removing Elements from a Slice
Problems
Chapter-9
Maps in Go
9.1 Maps
9.1.1 Key Data Type
9.1.2 How to Check Whether a Key Exists or Not?
9.1.3 Map Creation Using make() Function
9.1.4 Nil Map
9.1.5 Removing an Element from a Map
9.1.6 Map Variables
9.1.7 Accessing the Elements of a Map Using For-Loop
Problems
Chapter-10
Pointers in Go
10.1 Introduction
10.2 Address Operator
10.3 Pointer Arithmetic in Go
10.4 Pointer Pointing Another Pointer
10.5 Pointers in Function Arguments
10.6 Functions Returning Pointers
10.7 Function Pointers
10.8 Array of Pointers
10.9 Pointer to a Struct
10.10 Creating Pointers by the New Function
10.11 Make Function for Memory Allocation
Problems
Chapter-11
Concurrency in Go
11.1 Go Routines
11.2 Wait for all Go-Routines to Finish
11.3 Go Channels
11.3.1 Buffered Channel in Go
11.3.2 Using a Loop with a Channel
11.3.3 Range and Close
11.4 Select Statement in Go
11.5.1 Race Condition for Go-Routines
11.5.2 Atomic Operations in Golang
Problems
Chapter-12
File Operations in Go
12.1 File Functions
12.2 Empty File Creation
12.3 Check if a File Exists in Go
12.4 Deleting a File in Go
12.5 Getting File Info
12.6 Opening Files
12.6.1 Open() Function
12.6.2 OpenFile()Function
12.7 Writing to File
12.7.1 Conventional Approach Using the os Package
12.7.2 File Writing Using the ioutil Package
12.7.3 File Writing Using the io Package
12.7.4 Buffered File Writing
12.7.4 Writing Binary Data to a File
12.8 File Reading
12.8.1 Reading All the File Using the ioutil Package
12.8.2 Read File by Chunks
12.8.3 Reading Binary File
Bibliography
Index
About the Author
Chapter-1
Data Types in Go Language
Abstract: In this chapter, we explain the data types used in Go programming. We use built-in functions Printf() and Println() to display the values to the screen. Go is a case sensitive programming language, and it has automatic garbage collector. Unused variables in go programming are not allowed even if they are used for illustrative purposes.
1.1 How to Write a Go Program
Every go program has a main function. For this reason, we first write the structure of the main function as in Code-1.1.
––––––––
Code 1.1
func main(){
}
Inside the main() function, we can write go statements. We write a statement to display the Hello World!
sentence to the screen as in Code-1.2.
––––––––
Code 1.2
func main(){
fmt.Println(Hello World!
)
}
––––––––
The Println() function used in Code-1.2 is defined in the package fmt
, we include this package using import keyword as in Code-1.3.
Code 1.3
import fmt
func main(){
fmt.Println(Hello World!
)
}
Finally, we import a special package called main which contains the code that can be built into an executable application, and we get the final form of the program as in Code-1.4.
Code 1.4
package main
import fmt