Download Complete (Ebook) Go Standard Library Cookbook by Radomir Sohlich ISBN 9781788475273, 1788475275 PDF for All Chapters
Download Complete (Ebook) Go Standard Library Cookbook by Radomir Sohlich ISBN 9781788475273, 1788475275 PDF for All Chapters
com
https://ebooknice.com/product/go-standard-library-
cookbook-23378366
DOWLOAD EBOOK
ebooknice.com
ebooknice.com
ebooknice.com
ebooknice.com
(Ebook) The C++ Standard Library by Nicolai M. Josuttis
ISBN 9780321623218, 0321623215
https://ebooknice.com/product/the-c-standard-library-6761728
ebooknice.com
https://ebooknice.com/product/python-standard-library-nutshell-
handbooks-1990292
ebooknice.com
https://ebooknice.com/product/the-python-standard-library-by-
example-34854700
ebooknice.com
https://ebooknice.com/product/net-framework-standard-library-
annotated-reference-972416
ebooknice.com
https://ebooknice.com/product/the-c-standard-library-fourth-edition-
includes-c-23-49417210
ebooknice.com
Go Standard Library Cookbook
Radomír Sohlich
BIRMINGHAM - MUMBAI
Go Standard Library Cookbook
Copyright © 2018 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form
or by any means, without the prior written permission of the publisher, except in the case of brief quotations
embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented.
However, the information contained in this book is sold without warranty, either express or implied. Neither the
author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to
have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products
mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy
of this information.
ISBN 978-1-78847-527-3
www.packtpub.com
mapt.io
Mapt is an online digital library that gives you full access to over 5,000 books and videos, as
well as industry leading tools to help you plan your personal development and advance
your career. For more information, please visit our website.
Why subscribe?
Spend less time learning and more time coding with practical eBooks and Videos
from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
PacktPub.com
Did you know that Packt offers eBook versions of every book published, with PDF and
ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a
print book customer, you are entitled to a discount on the eBook copy. Get in touch with us
at service@packtpub.com for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a
range of free newsletters, and receive exclusive discounts and offers on Packt books and
eBooks.
Contributors
In 2015, he fell in love with Go and kept exploring the endless power and possibilities of the
language. He is passionate about learning new approaches and technology and feels the
same about sharing the knowledge with others.
I'd like to thank my beloved wife and kids for the time they gave me for creating this book.
The next big thank you belongs to Mert Serin, who agreed to review the book and give a
feedback on the content.
About the reviewer
Mert Serin was born in Izmir in 1993, graduated from Hacettepe University in 2016, and
has worked on iOS development since his third year in university. He is currently working
at USIT Inc., a start-up based in Atlanta, GE, as a full stack developer.
How it works… 27
See also 28
Retrieving child process information 28
Getting ready 28
How to do it… 28
How it works… 31
See also 31
Reading/writing from the child process 31
Getting ready 31
How to do it… 32
How it works… 38
Shutting down the application gracefully 39
How to do it… 39
How it works… 42
See also 42
File configuration with functional options 42
How to do it... 42
How it works... 45
Chapter 2: Strings and Things 46
Introduction 46
Finding the substring in a string 47
How to do it... 47
How it works... 48
See also 48
Breaking the string into words 49
How to do it... 49
How it works... 52
There's more... 53
Joining the string slice with a separator 53
How to do it... 53
How it works... 55
There's more... 56
Concatenating a string with writer 56
How to do it... 56
How it works... 58
[ ii ]
Table of Contents
There's more... 58
Aligning text with tabwriter 59
How to do it... 60
How it works... 61
Replacing part of the string 61
How to do it... 61
How it works... 63
There's more... 64
Finding the substring in text by the regex pattern 64
How to do it... 64
How it works... 65
See also 66
Decoding a string from the non-Unicode charset 66
How to do it... 66
How it works... 68
Controlling case 69
How to do it... 69
How it works... 71
Parsing comma-separated data 71
How to do it... 72
How it works... 74
Managing whitespace in a string 75
How to do it... 75
How it works... 76
See also 77
Indenting a text document 77
How to do it... 78
How it works... 79
See also 79
Chapter 3: Dealing with Numbers 80
Introduction 80
Converting strings to numbers 81
How to do it... 81
How it works... 82
Comparing floating-point numbers 83
[ iii ]
Table of Contents
How to do it... 83
How it works... 85
Rounding floating-point numbers 86
How to do it... 86
How it works... 87
Floating-point arithmetics 88
How to do it... 88
How it works... 89
There's more... 89
See also 90
Formatting numbers 90
How to do it... 90
How it works... 92
There's more... 93
Converting between binary, octal, decimal, and hexadecimal 93
How to do it... 93
How it works... 95
Formatting with the correct plurals 95
Getting ready 95
How to do it... 95
How it works... 97
There's more... 97
Generating random numbers 98
How to do it... 98
How it works... 99
Operating complex numbers 100
How to do it... 100
How it works... 101
Converting between degrees and radians 102
How to do it... 102
How it works... 103
Taking logarithms 104
How to do it... 104
How it works... 105
Generating checksums 105
[ iv ]
Visit https://ebooknice.com to
discover a wide range of
eBooks across various genres.
Enjoy exclusive deals and
discounts to enhance your
reading experience. Start your
digital reading journey today!
Table of Contents
[v]
Table of Contents
[ vi ]
Table of Contents
[ vii ]
Table of Contents
[ viii ]
Table of Contents
[ ix ]
Table of Contents
[x]
Table of Contents
[ xi ]
Table of Contents
[ xii ]
Table of Contents
Index 308
[ xiii ]
Preface
Thanks for giving this book a chance! This book is a guide that takes you through the
possibilities of the Go standard library, which has a lot of out-of-the-box functionality and
solutions. Note that the solutions covered in the book are mainly simple demonstrations of
how the standard library implementation is used and how it works. These recipes are
meant to provide a starting point for you to work out how to solve your specific problem
rather than solve the problem completely.
Chapter 2, Strings and Things, goes through common operations on strings, from simple
searching for substrings to formatting text to tabs.
Chapter 3, Dealing with Numbers, sheds light on basic conversions and number formatting
options. Operations with large numbers and the correct use of plurals within output
messages are covered.
Chapter 4, Once Upon a Time, puts the time package under the magnifying glass and covers
formatting, arithmetics, and running code for given time period or after a certain delay.
Chapter 5, In and Out, covers I/O operations that utilize standard Go interfaces. Besides the
basic I/O, the chapter also covers some useful serialization formats and how to handle them.
Visit https://ebooknice.com to
discover a wide range of
eBooks across various genres.
Enjoy exclusive deals and
discounts to enhance your
reading experience. Start your
digital reading journey today!
Preface
Chapter 6, Discover the Filesystem, discusses working with the filesystem, including listing
the folders, reading and changing the file attributes, and comparing files side by side.
Chapter 7, Connect the Network, showcases the client-side implementations for connecting
the TCP and UDP server, along with the use of SMTP, HTTP, and JSON-RPC .
Chapter 8, Working with Databases, focuses on common database tasks such as data selection
and extraction, transaction handling and execution, and the shortcomings of stored
procedures.
Chapter 9, Come to the Server Side, provides a view on networking from the server's
perspective. TCP, UDP, and HTTP server basics are presented.
Chapter 10, Fun with Concurrency, deals with mechanisms of synchronization and
concurrent access to resources.
Chapter 11, Tips and Tricks, comes with useful tips for testing and improving the HTTP
server implementation and shows the benefits of HTTP/2 push.
Unix-based environment
A version of Go equal to or greater than 1.9.2
An internet connection
Read, write, and execute permissions on a folder where the sample code will be
created and executed
[2]
Preface
Once the file is downloaded, please make sure that you unzip or extract the folder using the
latest version of:
We also have other code bundles from our rich catalog of books and videos available
at https://github.com/PacktPublishing/. Check them out!
Conventions used
There are a number of text conventions used throughout this book.
CodeInText: Indicates code words in text, database table names, folder names, filenames,
file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an
example: "Verify that your GOPATH and GOROOT environmental variables are set properly."
[3]
Preface
When we wish to draw your attention to a particular part of a code block, the relevant lines
or items are set in bold:
package main
import (
"log"
"runtime"
)
Bold: Indicates a new term, an important word, or words that you see onscreen. For
example, words in menus or dialog boxes appear in the text like this.
Sections
In this book, you will find several headings that appear frequently (Getting ready, How to do
it..., How it works..., There's more..., and See also).
To give clear instructions on how to complete a recipe, use these sections as follows:
Getting ready
This section tells you what to expect in the recipe and describes how to set up any software
or any preliminary settings required for the recipe.
How to do it…
This section contains the steps required to follow the recipe.
How it works…
This section usually consists of a detailed explanation of what happened in the previous
section.
[4]
Preface
There's more…
This section consists of additional information about the recipe in order to make you more
knowledgeable about the recipe.
See also
This section provides helpful links to other useful information for the recipe.
Get in touch
Feedback from our readers is always welcome.
General feedback: Email feedback@packtpub.com and mention the book title in the
subject of your message. If you have questions about any aspect of this book, please email
us at questions@packtpub.com.
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes
do happen. If you have found a mistake in this book, we would be grateful if you would
report this to us. Please visit www.packtpub.com/submit-errata, selecting your book,
clicking on the Errata Submission Form link, and entering the details.
Piracy: If you come across any illegal copies of our works in any form on the internet, we
would be grateful if you would provide us with the location address or website name.
Please contact us at copyright@packtpub.com with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in
and you are interested in either writing or contributing to a book, please visit
authors.packtpub.com.
Reviews
Please leave a review. Once you have read and used this book, why not leave a review on
the site that you purchased it from? Potential readers can then see and use your unbiased
opinion to make purchase decisions, we at Packt can understand what you think about our
products, and our authors can see your feedback on their book. Thank you!
[5]
Interacting with the
1
Environment
In this chapter, the following recipes are covered:
Introduction
Every program, once it is executed, exists in the environment of the operating system. The
program receives input and provides output to this environment. The operating system also
needs to communicate with the program to let it know what's happening outside. And
finally, the program needs to respond with appropriate actions.
Interacting with the Environment Chapter 1
This chapter will walk you through the basics of the discovery of the system environment,
the program parameterization via program arguments, and the concept of the operating
system signals. You will also learn how to execute and communicate with the child process.
Besides the build version and, for example, the environmental variables, the Go version by
which the binary was compiled could be included in the log. The following recipe will show
you how to include the Go runtime version into such program information.
Getting ready
Install and verify the Go installation. The following steps could help:
How to do it...
The following steps cover the solution:
package main
import (
"log"
"runtime"
)
const info = `
[7]
Interacting with the Environment Chapter 1
Application %s starting.
The binary was build by GO: %s`
func main() {
log.Printf(info, "Example", runtime.Version())
}
How it works...
The runtime package contains a lot of useful functions. To find out the Go runtime version,
the Version function could be used. The documentation states that the function returns the
hash of the commit, and the date or tag at the time of the binary build.
This .go file is generated by the go dist tool and the version is resolved by
the findgoversion function in the go/src/cmd/dist/build.go file, as explained next.
The $GOROOT/VERSION takes priority. If the file is empty or does not exist,
the $GOROOT/VERSION.cache file is used. If the $GOROOT/VERSION.cache is also not
found, the tool tries to resolve the version by using the Git information, but in this case, you
need to initialize the Git repository for the Go source.
[8]
Interacting with the Environment Chapter 1
Simply, the parameterized program call could look like this: ./parsecsv user.csv
role.csv. In this case, parsecsv is the name of the executed binary and user.csv and
role.csv are the arguments, that modify the program call (in this case it refers to files to be
parsed).
How to do it...
1. Open the console and create the folder chapter01/recipe02.
2. Navigate to the directory.
3. Create the main.go file with the following content:
package main
import (
"fmt"
"os"
)
func main() {
args := os.Args
[9]
Interacting with the Environment Chapter 1
}
}
How it works...
The Go standard library offers a few ways to access the arguments of the program call. The
most generic way is to access the arguments by the Args variable from the OS package.
This way you can get all the arguments from the command line in a string slice. The
advantage of this approach is that the number of arguments is dynamic and this way
you can, for example, pass the names of the files to be processed by the program.
The preceding example just echoes all the arguments that are passed to the program.
Finally, let's say the binary is called test and the program run is executed by the Terminal
command ./test arg1 arg2.
In detail, the os.Args[0] will return ./test. The os.Args[1:] returns the rest of the
arguments without the binary name. In the real world, it is better to not rely on the number
of arguments passed to the program, but always check the length of the argument array.
Otherwise, naturally, if the argument on a given index is not within the range, the program
panics.
[ 10 ]
Exploring the Variety of Random
Documents with Different Content
The Project Gutenberg eBook of Over the
Santa Fé Trail, 1857
This ebook is for the use of anyone anywhere in the United States
and most other parts of the world at no cost and with almost no
restrictions whatsoever. You may copy it, give it away or re-use it
under the terms of the Project Gutenberg License included with this
ebook or online at www.gutenberg.org. If you are not located in the
United States, you will have to check the laws of the country where
you are located before using this eBook.
Language: English
BY
W. B. NAPTON.
1905.
FRANKLIN HUDSON PUBLISHING CO.,
KANSAS CITY, MO.
CONTENTS
Page
I. Captain “Jim Crow” Chiles 3
II. In Camp, South of Westport 10
III. Buffalo 14
IV. Companions of Voyage 18
V. Pestiferous Indians 21
VI. At the Kiowa Camp 28
VII. To the Cimarron 33
VIII. My First Antelope 38
IX. A Kicking Gun and a Bucking Mule 46
X. A Gray Wolf 50
XI. Arrival at Las Vegas 54
XII. In Peril of Indians 62
XIII. Captain Chiles’ Chase 69
I.
Captain “Jim Crow” Chiles.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebooknice.com