Playlists
History
Topics
Tutorials
Offers & Deals
Highlights
Settings
Support
Sign Out
Let's Get Going
1 syntax basics
istory
opics
utorials
ffers & Deals
ighlights
ettings
Support
Sign Out
a
you
ready
your
simple
a
you
want
Do
Are
to
software?
turbo-charge
your
compiles
easy
runs
fast?
fast?
to
language
programming
makes
it
That
That
that
you're
distribute
for
Go!
ready
users?
work
Then
to
simplicity
speed.
programming
and
focuses
that
language
on
much
It's
less
Go
is
complex
languages,
make
lets
learn.
other
use
full
you
it
to
quicker
it's
so
than
And
of
This
programs
computer
today's
so
processors,
multicore
your
run
faster.
chapter
will
a
life
your
developer
as
easier,
make
Go
show
and
that
you
features
the
all
will
READY, SET, GO!
Back in 2007, the search engine Google had a problem. They had to maintain programs
with millions of lines of code. Before they could test new changes, they had to compile
the code into a runnable form, a process which at the time took the better part of an
hour. Needless to say, this was bad for developer productivity.
So Google engineers Robert Griesemer, Rob Pike, and Ken Thompson sketched out
some goals for a new language:
• Fast compilation
• Less cumbersome code
• Unused memory freed automatically (garbage collection)
• Easy to write software that does several operations simultaneously (concurrency)
• Good support for processors with multiple cores
After a couple years of work, Google had a language that was fast to write code for and
produced programs that were fast to compile and run. The project switched to an open
source license in 2009. Go is now free for anyone to use. And you should use it! Go is
rapidly gaining popularity thanks to its simplicity and power.
If you're writing a commandline tool, Go can produce executable files for Windows,
Mac, and Linux, all from the same source code. If you're writing a web server, Go can
help you handle many users connecting at once. And no matter what you're writing, it
will help you ensure that your code is easier to maintain and add to.
Ready to learn more? Let's Go!
THE GO PLAYGROUND
The easiest way to try Go is to visit https://play.golang.org in your web browser. There,
the Go team has set up a simple editor where you can enter Go code and run it on their
servers. The result is displayed right there in your browser.
(Of course, this only works if you have a stable Internet connection. If you don't, see
page 25 to learn how to download and run the Go compiler directly on your computer.
Then run the following examples using the compiler instead.)
Let's try it out now!
Open http://play.golang.org in your browser. (Don't worry if what you see doesn't
quite match the screenshot; it just means they've improved the site since this book was
printed!)
Delete any code that's in the editing area, and type this instead:
Click the "Format" button, which will automatically reformat your code according to
Go conventions.
Click the "Run" button.
You should see "Hello, Go!" displayed at the bottom of the screen. Congratulations,
you've just run your first Go program!
Turn the page, and we'll explain what we just did...
WHAT DOES IT ALL MEAN?
You've just run your first Go program! Now let's look at the code and figure out what it
actually means...
Every Go file starts with a package declaration. A package is a group of code that all
does similar things, like formatting strings or drawing images. The package declaration
gives the name of the package that this file's code will become a part of. In this case, we
use the special package main, which is required if this code is going to be run directly
(usually from the terminal).
Next, Go files almost always have one or more import statements. Each file needs to
import other packages before its code can use the code those other packages contain.
Loading all the Go code on your computer at once would result in a big, slow program,
so instead you specify only the packages you need by importing them.
The last part of every Go file is the actual code, which is often split up into one or more
functions. A function is a group of one or more lines of code that you can call (run)
from other places in your program. When a Go program is run, it looks for a function
named main and runs that first, which is why we named this function main.
Don't worry if you don't understand all this right now!
We'll look at everything in more detail in the next few pages.
The typical Go file layout
You'll quickly get used to seeing these three sections, in this order, in almost every Go
file you work with:
1. The package declaration
2. Any import statements
3. The actual code
The saying goes, "a place for everything, and everything in its place." Go is a very
consistent language. This is a good thing: you'll often find you just know where to look
in your project for a given piece of code, without having to think about it!
THERE ARE NO DUMB QUESTIONS
Q: My other programming language requires that each statement end with
a semicolon. Doesn't Go?
A: You can use semicolons to separate statements in Go, but it's not required (in fact,
it's generally frowned upon).
Q: Why did we run the automatic reformatting tool on our code?
A: Whenever you share your code, other Go developers will expect it to be in the
standard Go format. That means that things like indentation and spacing will be
formatted in a standard way, making it easier for everyone to read. Where other
languages achieve this by relying on people manually reformatting their code to
conform to a style guide, with Go all you have to do is run the standard formatting tool,
and it will automatically fix everything for you. We ran the formatter on every example
we created for this book, and you should run it on all your code, too!
WHAT IF SOMETHING GOES WRONG?
Go programs have to follow certain rules to avoid confusing the compiler. If we break
one of these rules, we'll get an error message.
Suppose we forgot to add parentheses on our call to the Println function on line 6:
If we try to run this version of the program, we get an error:
Go tells us the name of the source code file and the line number where there's a
problem. (The Go Playground saves the contents of the online editor to a temporary file
before running it, which is where the "main. go" file name comes from.) Then it gives a
description of the error. In this case, because we deleted the parenthesis, Go can't tell
we're trying to call the Println function, so it can't understand why we're putting "Hello,
Go" at the end of line 6.
BREAKING STUFF IS EDUCATIONAL!
We can get a feel for the rules Go programs have to follow by intentionally breaking our
program in various ways. Take this code sample, try making one of the changes below,
and run it. Then undo your change, and try the next one. See what happens!
If you do this...
...it will fail because...
Delete the package
declaration... package
main
Every Go file has to begin with a package declaration.