Learning Go Programming 1st Edition Vladimir Vivien - Download the full ebook set with all chapters in PDF format
Learning Go Programming 1st Edition Vladimir Vivien - Download the full ebook set with all chapters in PDF format
com
https://ebookmeta.com/product/learning-go-programming-1st-
edition-vladimir-vivien/
OR CLICK HERE
DOWLOAD EBOOK
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, and its dealers and
distributors will be held liable for any damages caused or alleged to
be caused directly or indirectly by this book.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78439-543-8
www.packtpub.com
Credits
Reviewers
Quintessence Anx
Project Coordinator
Parth Desai
Nidhi Joshi
Abhishek Kumar
Chris Schaefer
He has also written custom single sign on solution (SSO) in c#, with
proper implementation of oauth and saml protocols. He also
developed pluggable architecture to facilitate easier adding and
removing of authentication providers for the SSO. Currently, He is
working as a lead backend engineer, and designing and
implementing scalable systems in golang and python. You can reach
out to him on linkedin at https://in.linkedin.com/in/parthdesai08. Or
on his email-id [email protected].
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 [email protected] for more details.
https://www.packtpub.com/mapt
Get the most in-demand software skills with Mapt. Mapt gives you
full access to all Packt books and video courses, as well as industry-
leading tools to help you plan your personal development and
advance your career.
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Preface
Go is an open source programming language that lets programmers
easily build reliable and scalable programs. It does this by offering a
simple syntax which makes it fun to write correct and predictable
code using concurrency idioms and a robust standard library.
Go has a large and active online community and there are several
Go conferences that take place around the world yearly. Starting
with https://golang.org/, you will find numerous places on the web
that provide documentations, blogs, videos, and slides that cover a
wide range of Go-related topics. On GitHub, the story is no different;
some of the best known projects that are driving the future of cloud
computing, for instance, are written in Go with an ever growing list.
As you would expect, getting started with Go is simple, fast, and well
documented. However, “getting into” Go can be more challenging,
especially for newcomers from other languages. My first attempt at
Go failed. Even after reading the prescribed documentations and
going through the tutorials, there was a gap in understanding driven
by my own biases from previous programming experiences. Months
later I returned to Go and got into it. This time I read the language
specs, I read blogs, watch videos, and searched the web for any
discussion that provided design motivations and in-depth
explanations of the language.
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
$> go version
go version go1.6.1 linux/amd64
New terms and important words are shown in bold. Words that you
see on the screen, for example, in menus or dialog boxes, appear in
the text like this: "If all goes well, you should see the message Hello,
World! output on your screen.."
Note
Warnings or important notes appear in a box like this.
Tip
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what
you think about this book-what you liked or disliked. Reader
feedback is important for us as it helps us develop titles that you will
really get the most out of. To send us general feedback, simply e-
mail [email protected], and mention the book's title in the
subject of your message. If there is a topic that you have expertise in
and you are interested in either writing or contributing to a book, see
our author guide at www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we have a
number of things to help you to get the most from your purchase.
Once the file is downloaded, please make sure that you unzip or
extract the folder using the latest version of:
Errata
Although we have taken every care to ensure the accuracy of our
content, mistakes do happen. If you find a mistake in one of our
books-maybe a mistake in the text or the code-we would be grateful
if you could report this to us. By doing so, you can save other
readers from frustration and help us improve subsequent versions of
this book. If you find any errata, please report them by visiting
http://www.packtpub.com/submit-errata, selecting your book, clicking
on the Errata Submission Form link, and entering the details of
your errata. Once your errata are verified, your submission will be
accepted and the errata will be uploaded to our website or added to
any list of existing errata under the Errata section of that title.
Piracy
Piracy of copyrighted material on the Internet is an ongoing problem
across all media. At Packt, we take the protection of our copyright
and licenses very seriously. If you come across any illegal copies of
our works in any form on the Internet, please provide us with the
location address or website name immediately so that we can
pursue a remedy.
Questions
If you have a problem with any aspect of this book, you can contact
us at [email protected], and we will do our best to address
the problem.
Chapter 1. A First Step in Go
In the first chapter of the book, you will be introduced to Go and take
a tour of the features that have made the language a favorite among
its adopters. The start of the chapter provides the motivation behind
the Go programming language. If you are impatient, however, you
are welcome to skip to any of the other topics and learn how to write
your first Go program. Finally, the Go in a nutshell section provides a
high-level summary of the characteristics of the language.
No IDE required
Besides the Go Playground, how is one supposed to write Go code
anyway? Writing Go does not require a fancy Integrated
Development Environment (IDE). As a matter of fact, you can get
started writing your simple Go programs with your favorite plain text
editor that is bundled with your OS. There are, however, Go plugins
for most major text editors (and full-blown IDEs) such as Atom, Vim,
Emacs, Microsoft Code, IntelliJ, and many others. There is a
complete list of editors and IDE plugins for Go which can be found at
https://github.com/golang/go/wiki/IDEsAndTextEditorPlugins.
Installing Go
To start programming with Go on your local machine you will need to
install the Go Toolchain on your computer. At the time of writing, Go
comes ready to be installed on the following major OS platforms:
Linux
FreeBSD Unix
Mac OSX
Windows
The official installation packages are all available for 32-bit and 64-
bit Intel-based architectures. There are also official binary releases
that are available for ARM architectures as well. As Go grows in
popularity, there will certainly be more binary distribution choices
made available in the future.
$> go version
go version go1.6.1 linux/amd64
The previous command should print the version number, target OS,
and the machine architecture where Go and its tools are installed. If
you do not get an output similar to that preceding command, ensure
to add the path of the Go binaries to your OS's execution PATH
environment variable.
Before you start writing your own code, ensure that you have
properly set up your GOPATH. This is a local directory where your Go
source files and compiled artifacts are saved as you use the Go
Toolchain. Follow the instructions found in
https://golang.org/doc/install#testing to set up your GOPATH.
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
golang.fyi/ch01/helloworld.go
If all goes well, you should see the message Hello, World! output on
your screen. Congratulations, you have just written and executed
your first Go program. Now, let us explore the attributes and
characteristics of the Go language at a high level.
Go in a nutshell
By design, Go has a simple syntax. Its designers wanted to create a
language that is clear, concise, and consistent with few syntactic
surprises. When reading Go code, keep this mantra in mind: what
you see is what it is. Go shies away from a clever and terse coding
style in favor of code that is clear and readable as exemplified by the
following program:
import "fmt"
// finds # of moles
func moles(mass amu) float64 {
return float64(mass) / grams
}
func main() {
fmt.Print(headers())
golang.fyi/ch01/metalloids.go
Functions
Go programs are composed of functions, the smallest callable code
unit in the language. In Go, functions are typed entities that can
either be named (as shown in the previous example) or be assigned
to a variable as a value:
// a simple Go function
func moles(mass amu) float64 {
return float64(mass) / grams
}
Title: Patsy
Language: English
London
Adelphi Terrace
First Edition 1908
Second 1909
Impression
Third 1909
Impression
Fourth 1910
Impression
Fifth 1916
Impression
Sixth 1917
Impression
A flock of wild geese flying across the sunset far away, remote,
fantastic, the only living things visible in a world filling with shadows,
lent the last touch of beauty to the vast and lonely moors.
“They’re makin’ for the pools of Cloyne, sir,” said the keeper.
Mr Fanshawe watched the flock pass and vanish in the amber
distance like a wreath of smoke. Away to the left, covering
themselves with night and gloom, stood the hills of Glynn, where the
golden eagle still has its eyrie, and the wild goat its home. From
there, away to the west, the great moors stretched to the hills of
Cloyne.
It was a typical Irish winter’s evening, the sky threatening and
forgetting to rain, the air damp and filled with the scent of the earth,
near things indistinct in the gathering twilight, and far things
seeming near.
From where Mr Fanshawe stood, with his pipe in his mouth and
his gun under his arm, you might have started with a brave heart to
walk to the hills of Cloyne. Ten miles distant, or at most twelve, they
seemed, those hills that lay thirty Irish miles away.
Fanshawe was staying for the hunting with Mr Trench of
Dunboyne House. He had come out to-day to have a shot at the
snipe, and he had not done badly, to judge by the weight of the bag
Micky Finn, the old keeper, was carrying.
“Well,” said the young man, refilling and lighting his pipe, “we’d
better be getting back. How far are we from the house, Micky?”
“A matter of five mile be the boggs, sir, an’ siven be the road;
which way would your ’arner be chusin’ to take?”
“The road,” said Mr Fanshawe, and, followed by Micky and the
dogs, he struck towards the high-road from Dunbeg which goes
across the moors white and straight like a chalk-line drawn by a
giant.
“You were afther askin’ me, sir, what time the letters came from
Dunbeg,” said Micky, as they stepped on to the highway. “Here’s
Larry and the letters now, comin’ as hard as he can pelt two hours
late, the blackguyard! He’s been stoppin’ to drink at Billy Sheehan’s,
or colloguing wid the girls; musha, but it’s little he cares who waits
for their letters whin the bottle’s before him.”
Mr Fanshawe shaded his eyes, and with a constriction of the heart
watched the horseman and the horse coming at a furious pace and
developing with magical speed against the sunset. The sound of the
hoofs, like the sound of castanets in the hands of a madman, came
on the breeze.
The horseman, a ragged individual with a leer on his face, no
boots on his feet, and a post-bag slung on his back, reined in when
he came level with the keeper and the gentleman, bringing his horse
literally on its haunches.
“Any letters for Mr Fanshawe, Larry?” asked the keeper.
“Begob!” said Larry, swinging the post-bag round and opening it,
“there’s letters enough for a dozen, but I’m no schollard to tell yiz
who thir for; will y’ be afther puttin’ your hand in the bag, sir, and
takin’ your chice?”
Mr Fanshawe did as he was invited. There was only one letter for
him, all the rest were for Mr Trench or members of his household.
It was not the letter he had been half expecting by every post for
weeks and weeks past, and he opened it with a gloomy brow, and
read it by the light of sunset as Larry rode on and the sound of the
hoofs died away on the high-road.
To be young, rich, healthy, good-looking, and yet unhappy! No
other magician but Love could bring about such an extraordinary
concatenation of states.
Love had done this in the case of Mr Fanshawe.
As for the letter, it was addressed from Glen Druid House, Tullagh,
Mid Meath, and it ran:
“Dear Richard,—I have only just been informed that you are staying with my
friends the Trenches, to whom, through you, I send my very kind regards.
“This house is only some forty miles from where you are now, and as I
have a small house-party coming on the 10th, the happy idea has occurred to
me that you might join us, if your engagements will permit you so to do. You
will find shooting enough to please you, I think, in the coverts, and the
O’Farrel’s hounds meet twice a week. You will also find a sincere welcome
from your old friend,
“Selina Seagrave.
“P.S.—I am here, at present, by myself. I would be quite alone were it not
that I have your cousin Robert’s children staying with me. Bob (Lord Gawdor),
Doris, and Selina my namesake.”
PATSY
“Miss Kiligrew,” said little Lord Gawdor, looking up from his slate
and the multiplication sum on it that wouldn’t come right.
“Yes, Robert?”
“William, the page-boy, was sent away yesterday for stealing the
jam.”
“Go on with your sum,” answered the governess, who was seated
at the other end of the table helping Doris, little Lord Gawdor’s
sister, to make a map. “This is the third time you have interrupted
me with frivolous remarks. How can you expect to make your sum
come right if you do not fix your mind on your slate?”
“I will in a minit,” said his lordship; “but I want to tell you, he’d
cribbed a pot of plum jam, and he heard some one coming, and he
popped it in the copper in the back kitchen where the clothes were
boiling. Gran’ma said she never heard of an act of such—what was
it, Doris?”
“Turpentine, I think,” replied Doris, throwing back her golden hair
from her forehead, relieved to escape for a moment from the
monotony of map-making.
“‘Turpitude,’ I suppose you mean,” said Miss Kiligrew.
“Yes, that was it,” said Lord Gawdor. “What’s it mean?”
“Wickedness,” replied Miss Kiligrew. “Go on with your sum.”
“I will; but I want just to tell you, he went away yesterday, and
gran’ma said to Mrs Kinsella the cook she didn’t know what she’d do
for a page-boy, and cook said she’d try and get Patsy Rooney, the
son of the keeper, to come. He’s that red-headed boy we saw
carrying the rabbits in the park the other day. My eye!” he concluded
with a burst of laughter, “won’t he look funny in buttons!”
“Go on with your sum,” said Miss Kiligrew severely, “and don’t use
vulgar expressions before your sister. Who taught you to say that?”
“What?”
“My eye.”
“William, I b’lieve.”
“Well, it is a very good thing he was sent about his business. Go
on with your sum.”
Lord Gawdor did as he was bid, and there was silence for a while,
broken only by the squeaking of his pencil on the slate and an
occasional clicking sound from under the table, where Selina, his
youngest sister, aged five, was seated on the floor playing with a box
of bricks. They were in the day nursery, which was also the
schoolroom, of Glen Druid Park, a great old Irish country house.
Little Lord Gawdor’s mother was dead and his father was in India.
He and his sisters were living with their grandmother, Lady
Seagrave. It was three weeks before Christmas, and as Lady
Seagrave had invited a house-party, the house was in a state of
upset owing to the preparations. Downstairs rooms were being
cleaned and dusted, carpets taken up and shaken, mirrors polished,
and mattresses standing to air before huge fires.
All the fun of a general house-cleaning was going on, and it
seemed very hard to Lord Gawdor and Doris that they had to sit all
the morning doing sums and making maps instead of helping to
increase the confusion down below.
“I’ve done my sum,” said his lordship at last.
“When I have finished demarcating this frontier I will look at it,”
said Miss Kiligrew, who had a paint brush in her hand, and was in
the act of tinting with red the boundary line between Cochin China
and Somewhere-else.
“All right,” said the boy; “don’t hurry, I can wait as long as you
like.” He left the chair and, going to the window, he climbed on to
the window-seat and looked out at the park. He had scarcely been a
minute at the window when he gave a cry.
“Miss Kiligrew—come here, quick!”
The governess and Doris left the table and came to the window.
“That’s him,” said Lord Gawdor, pointing to a small figure trudging
across the park.
“Who?” asked the governess.
“Patsy Rooney,” replied he.
“How dare you call me from my work to look at such nonsense!”
cried Miss Kiligrew. “Have you no regard for the value of my time?”
“Patsy isn’t nonsense,” replied his lordship. “They say he can trap
rabbits better than his father, and he keeps the ferrits and helps to
clean the guns, and,” finished up Lord Gawdor, dropping off the
window-seat and coming back wearily to the table, “I wish to
goodness I was him!”
CHAPTER III
It had snowed slightly in the early morning, and enough snow lay
on the ground to take the track of a hare.
The ground told quite a lot of things to Patsy Rooney as he made
his way across Glen Druid Park from his father’s cottage to the little
village of Castle Knock, which lies beyond the park a mile to the
west, where the Tullagh road meets the road to Kilgobbin.
Out in the open spaces the great feet of the crows had left their
mark clear cut in the snow. Crossing them you could see the lesser
traces of the ringed plover, and all sorts of little birds had left tiny
footprints where the snow lay thin and white as a sheet on the
borders of the beech woods.
All kinds of rare birds came to Glen Druid Park, for the place had
been deserted so long that there was no one to trouble them, except
Patsy’s father, who was the keeper, and who lived in the keeper’s
cottage close to the Big House.
The Big House had been deserted for years, but it was deserted
no longer, for only that autumn Lady Seagrave had taken it, and she
and her family had already moved in; and there were, as I have
hinted, to be great doings at Christmas, and the whole country-side
was talking of the wonderful things that were to happen when the
“quality” arrived.
By the “quality” the country people meant the guests who were
coming over from England. Lords and ladies were reputed to be
coming, and bringing their hunting horses with them, and there was
a rumour that a bishop was coming, too. Patsy was anxious enough
to see the lords and ladies, but he was more anxious still to see the
bishop; what such a thing was like he could not in the least imagine.
He could have asked, but he didn’t: firstly, because he was a person
of such little importance that no one would have been bothered
answering him; and secondly, because he did not want to spoil the
sight when it came by knowing what it would be like beforehand. He
thought it was some sort of animal.
He was going through the beech woods now at a “sweep’s trot” to
keep himself warm. He had an old stake plucked from a fence in his
hand, and as he ran he would every now and then twirl the stake
round his head and give a “whoop” that sent the startled birds
fluttering through the branches and the rabbits scuttling through the
withered fern.
He was not going through the thick of the wood, but down a
broad drive that was the shortest cut to the village; and he did not
twirl the stake round his head and whoop for the fun of the thing,
but to keep up his courage. For the drive was just the place where
the “carriage” was always met.
Patsy’s uncle had seen the “carriage,” or said he had. So had a lot
of other people. It was a hearse with plumes, driven by a man
without a head, and it was supposed to haunt the grounds of Glen
Druid Park, sometimes even in daylight.
The horrible thing about it was that when the man without the
head saw you, he made straight for you; and, if he overtook you,
down he would get and bundle you into the vehicle and drive off,
and then you were done for.
The snow on the drive, like the snow on the grass of the park,
showed all sorts of little footprints. Tracks of hares and rabbits and
the trail of a stoat, Patsy knew and could distinguish them all.
Though he could neither read or write, he knew the habits and
names of all the wild animals and birds that were to be seen in the
woods and ways around; he knew all the tales about the fairies that
lived under the ferns in the glens, and the cluricaunes that cobbled
the fairies’ boots. He had never seen a cluricaune or a fairy, but he
believed in them, notwithstanding the fact that he had a very sharp
and practical mind where the ordinary business of life was
concerned.
Suddenly Patsy came to a stand close to the trunk of a great
beech tree. He had caught a glimpse of something in the wood on
the right-hand side of the drive.
CHAPTER IV
CON COGAN