GetGoing/src/1-hello-world.md

195 lines
5.3 KiB
Markdown
Raw Normal View History

2019-10-28 22:38:30 +00:00
# Hello, World
2019-10-28 22:20:15 +00:00
2019-10-28 22:38:30 +00:00
In this chapter we will cover how to install Go and write a simple "Hello,
world!" program.
2019-10-28 22:20:15 +00:00
2019-10-28 22:38:30 +00:00
By the end of this chapter you should have:
2019-10-28 22:20:15 +00:00
2019-10-28 22:38:30 +00:00
- The Go programming language compiler installed
- A working "Hello, world!" program written
2019-10-28 22:20:15 +00:00
## What is Go?
Go is a compiled programming language made by Google. It has a lot of features
out of the box, including:
* A static type system
* Fast compile times
* Efficient code generation
* Parallel programming for free*
* A strong standard library
* Cross-compilation with ease (including webassembly)
* and more!
\* You still have to write code that can avoid race conditions, more on those
later.
### Why Use Go?
Go is a very easy to read and write programming language. Consider this snippet:
```go
func Add(x int, y int) int {
return x + y
}
```
This function wraps [integer
addition](https://golang.org/ref/spec#Arithmetic_operators). When you call it it
returns the sum of x and y.
## Installing Go
### Linux
Installing Go on Linux systems is a very distribution-specific thing. Please see
[this tutorial on
DigitalOcean](https://www.digitalocean.com/community/tutorials/how-to-install-go-on-ubuntu-18-04)
for more information.
### macOS
* Go to https://golang.org/dl
* Download the .pkg file
* Double-click on it and go through the installer process
### Windows
* Go to https://golang.org/dl
* Download the .msi file
* Double-click on it and go through the installer process
### Next Steps
These next steps are needed to set up your shell for Go programs.
Pick a directory you want to store Go programs and downloaded source code in.
This is called your GOPATH. This is usually the `go` folder in
your home directory. If for some reason you want another folder for this, use
that folder instead of `$HOME/go` below.
#### Linux/macOS
This next step is unfortunately shell-specific. To find out what shell you are
using, run the following command in your terminal:
```console
$ env | grep SHELL
```
The name at the path will be the shell you are using.
##### bash
If you are using bash, add the following lines to your .bashrc (Linux) or
.bash_profile (macOS):
```
export GOPATH=$HOME/go
2019-10-28 23:21:04 +00:00
export PATH="$PATH:$GOPATH/bin"
2019-10-28 22:20:15 +00:00
```
Then reload the configuration by closing and re-opening your terminal.
##### fish
If you are using fish, create a file in ~/.config/fish/conf.d/go.fish with the
following lines:
```
set -gx GOPATH $HOME/go
2019-10-28 23:21:04 +00:00
set -gx PATH $PATH "$GOPATH/bin"
2019-10-28 22:20:15 +00:00
```
##### zsh
If you are using zsh, add the following lines to your .zshrc:
```
export GOPATH=$HOME/go
2019-10-28 23:21:04 +00:00
export PATH="$PATH:$GOPATH/bin"
2019-10-28 22:20:15 +00:00
```
#### Windows
2019-10-28 23:21:04 +00:00
Follow the instructions
[here](https://github.com/golang/go/wiki/SettingGOPATH#windows).
## Installing a Text Editor
For this book, we will be using VS Code. Download and install it
from https://code.visualstudio.com. The default settings will let you work with
Go code.
2019-10-28 22:20:15 +00:00
## Hello, world!
Now that everything is installed, let's test it with the classic "Hello, world!"
2019-10-28 23:21:04 +00:00
program. Create a folder in your home folder `Code`. Create another folder
inside that Code folder called `get_going` and create yet another subfolder
called `hello`. Open a file in there with VS Code (Open Folder -> Code ->
get_going -> hello) called `hello.go` and type in the following:
2019-10-28 22:20:15 +00:00
```go
// Command hello is your first Go program.
package main
import "fmt"
func main() {
fmt.Println("Hello, world!")
}
```
This program prints "Hello, world!" and then immediately exits. Here's each of
the parts in detail:
```go
// Command hello is your first go program.
package main // Every go file must be in a package.
// Package main is used for creating executable files.
import "fmt" // Go doesn't implicitly import anything. You need to
// explicitly import "fmt" for printing text to
// standard output.
func main() { // func main is the entrypoint of the program, or
// where the computer starts executing your code
fmt.Println("Hello, world!") // This prints "Hello, world!" followed by a newline
// to standard output.
} // This ends the main function
```
2019-10-28 23:21:04 +00:00
Now click over to the terminal at the bottom of the VS Code window and run this
program with the following command:
2019-10-28 22:20:15 +00:00
```console
$ go run hello.go
Hello, world!
```
`go run` compiles and runs the code for you, without creating a persistent binary
file. This is a good way to run programs while you are writing them.
To create a binary, use `go build`:
```console
$ go build hello.go
$ ./hello
Hello, world!
```
`go build` has the compiler create a persistent binary file and puts it in the
same directory as you are running `go` from. Go will choose the filename of the
binary based on the name of the .go file passed to it. These binaries are
usually static binaries, or binaries that are safe to distribute to other
computers without having to worry about linked libraries.
2019-10-28 22:38:30 +00:00
## Exercises
2019-10-28 22:20:15 +00:00
2019-10-28 22:38:30 +00:00
That should do it for this chapter. Here are a few optional exercises to try:
2019-10-28 22:20:15 +00:00
2019-10-28 22:38:30 +00:00
1. Change the text that gets printed to greet you with your name.
2. Rename the file to `main.go`, does it still build?
3. Look through the documentation of the [fmt
package](https://golang.org/pkg/fmt) and see what it does.