This commit is contained in:
Cadey Ratio 2019-10-28 22:38:30 +00:00
parent 1e8d9c59f4
commit c4e8df367c
26 changed files with 51 additions and 296 deletions

View File

@ -3,25 +3,27 @@
files=' files='
src/title.md src/title.md
CHANGELOG.md CHANGELOG.md
src/intro.md
src/1-hello-world.md src/1-hello-world.md
src/2-data-tyes.md src/2-data-tyes.md
src/3-slices-maps.md src/3-slices-maps.md
src/4-control-structures.md src/4-control-structures.md
src/5-functions.md src/5-functions.md
src/6-structures.md src/6-structures.md
src/7-http.md sec/7-interfaces.md
src/8-testing.md src/8-http.md
src/9-cli-app.md src/9-testing.md
src/10-repo-hygiene.md src/10-cli-app.md
src/11-go-modules.md src/11-repo-hygiene.md
src/12-wiki.md src/12-go-modules.md
src/13-webapp-abstractions.md src/13-wiki.md
src/14-conclusion.md src/14-webapp-abstractions.md
src/15-conclusion.md
src/15-battlesnake.md src/15-battlesnake.md
src/GReeTZ.md src/GReeTZ.md
src/other_work_by_the_author.md src/other_work_by_the_author.md
' '
rev=$(git rev-parse HEAD) rev=$(git rev-parse HEAD --short)
pandoc -o getgoing pandoc -o getgoing-${rev}.epub $files

View File

@ -1,18 +1,12 @@
--- # Hello, World
sitename: Within
title: "Go Training 1: Get Going"
template: blog
---
This post is the first in a series I'm creating to help people learn the In this chapter we will cover how to install Go and write a simple "Hello,
[Go][go] programming language. It's aimed at people who understand the high world!" program.
level concepts of programming, but haven't had much practical experience with
it.
[go]: https://golang.org By the end of this chapter you should have:
Like always, feedback is very welcome. Any feedback I get will be used to help - The Go programming language compiler installed
make this course even better. - A working "Hello, world!" program written
## What is Go? ## What is Go?
@ -181,207 +175,11 @@ 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 usually static binaries, or binaries that are safe to distribute to other
computers without having to worry about linked libraries. computers without having to worry about linked libraries.
## Go Modules ## Exercises
[Go modules](https://github.com/golang/go/wiki/Modules) are the current state of That should do it for this chapter. Here are a few optional exercises to try:
the art of dependency and project management for programs written in Go. We will
cover them in much more detail in the future, but for now create a module for
your hello command with the following command:
```console 1. Change the text that gets printed to greet you with your name.
$ go mod init your-name.localhost/hello 2. Rename the file to `main.go`, does it still build?
go: creating new go.mod: module christine-dodrill.localhost/hello 3. Look through the documentation of the [fmt
``` package](https://golang.org/pkg/fmt) and see what it does.
This creates a file called go.mod that tells the compiler details about your
project. Again, we will cover this in more detail in the future, but just know
this exists for now.
## Creating Other Files
Go packages map to folders on the filesystem. This means that every .go file in
the same filesystem folder must be inside the same package in order for the
package to compile. There are some edge case exceptions to this rule, of course,
but those will be covered later.
### Adding Numbers
Create a new file called `math.go` with the following contents:
```
package main
// Add adds two numbers and returns the sum.
func Add(x, y int) int {
return x + y
}
```
This file creates a function called `Add` that adds the numbers it is given.
This function is also documented because there's a comment immediately above it
explaining what it does. We can view the documentation with the `go doc`
command:
```console
$ go doc -all
Command hello is your first Go program.
FUNCTIONS
func Add(x, y int) int
Add adds two numbers and returns the sum. This helps you understand functions.
```
Go automatically keeps track of documentation comments and lets you query them
with `go doc`. These comments should usually help explain _why_ things are the
way they are. That helps your future self understand what is going on there.
### Dividing Numbers
Go allows functions to return multiple values. When a function returns a result
but can also results in an error it is a common idiom to return two values: the
result and the error.
Unlike Java and Python, Go does not have have Exceptions that get thrown.
Instead, errors are regular values returned by functions.
We can model this idiom with integer division, where anything divided by zero is
famously undefined.
Add the following to `math.go`:
```go
// ErrDivideByZero is returned when Divide would divide by zero.
var ErrDivideByZero = errors.New("divide by zero")
// Divide performs floating-point division and returns the result. If the right
// hand side of Divide is zero, it returns ErrDivideByZero to avoid a panic.
func Divide(lhs, rhs float64) (float64, error) {
if rhs == 0 {
return 0, ErrDivideByZero
}
return lhs / rhs, nil
}
```
And add this to the top of `math.go`, just below the `package` line:
```go
import "errors"
```
We need to import the errors package to create the ErrDivideByZero error. This
error will be returned when the left-hand-side of the Divide function is zero.
### Weaving it Together
Now these functions in `math.go` are usable from `hello.go`. Let's add a few
numbers after the "Hello, world!" call.
```go
var sum int = Add(2, 2)
fmt.Println("2 + 2:", sum)
```
This creates an integer variable called sum and sets it to the result of adding
2 and 2 together. You can also create the sum variable by doing this:
```go
sum := Add(2, 2)
```
The `:=` operator automagically declares the variable on its left hand side with
the value on its right hand side.
Now run it with `go run .`. The `.` argument tells `go run` to use the package
in the current working directory.
```console
$ go run .
Hello, world!
2 + 2: 4
```
Let's add division. Add the following lines under the last fmt.Println call:
```go
quotient, err := Divide(4, 2)
if err != nil {
panic(err)
}
fmt.Println("4 / 2:", quotient)
```
And run it:
```console
$ go run .
Hello, world!
2 + 2: 4
4 / 2: 2
```
Now, what would happen if you divide something by zero? Let's find out by adding
the following after the last fmt.Println call:
```go
quotient, err = Divide(4, 0)
if err != nil {
panic(err)
}
fmt.Println("but the quotient of 4 / 0 was:", quotient)
```
The `:=` operator isn't used here because that would cause a compile error. This
error would happen because the last `:=` already defined the variables and names
cannot be redefined in Go.
Now let's run it:
```console
$ go run .
Hello, world!
2 + 2: 4
4 / 2: 2
panic: divide by zero
goroutine 1 [running]:
main.main()
/Users/christine.dodrill/Code/go_learning/hello/hello.go:20 +0x26c
exit status 2
```
This panics with the error "divide by zero" because of the call on line 20.
Let's change that panic to a println so we can see what the quotient would be:
```go
quotient, err = Divide(4, 0)
if err != nil {
fmt.Println("got an error:", err)
}
fmt.Println("but the quotient of 4 / 0 was:", quotient)
```
And run it:
```console
$ go run .
Hello, world!
2 + 2: 4
4 / 2: 2
got an error: divide by zero
but the quotient of 4 / 0 was: 0
```
The error was caught. This lets you handle errors with any custom logic you'd
need to write. Sometimes it could mean passing an error up to its caller,
sometimes it could mean retrying. It really depends on the context.
## Conclusion
And that about wraps it up for Lesson 1 in Go. Next we'll be covering making a
HTTP request and validating its response with icanhazip.com. Like I mentioned
before, feedback on this helps a lot.
Thanks and be well.

2
src/10-cli-app.md Normal file
View File

@ -0,0 +1,2 @@
# Writing a Command-Line Application

View File

@ -1,6 +0,0 @@
---
sitename: Within
title: "Go Training 10: Repo Hygiene"
template: blog
---

View File

@ -1,6 +0,0 @@
---
sitename: Within
title: "Go Training 11: Go Modules"
template: blog
---

1
src/11-repo-hygiene.md Normal file
View File

@ -0,0 +1 @@
# Repo Hygiene

1
src/12-go-modules.md Normal file
View File

@ -0,0 +1 @@
# Go Modules

View File

@ -1,6 +0,0 @@
---
sitename: Within
title: "Go Training 12: Putting it All Together with a Wiki"
template: blog
---

View File

@ -1,6 +0,0 @@
---
sitename: Within
title: "Go Training 13: Webapp Abstractions"
template: blog
---

1
src/13-wiki.md Normal file
View File

@ -0,0 +1 @@
# Putting it All Together with a Wiki

View File

@ -1,5 +0,0 @@
---
title: "Go Training 14: Conclusion"
sitename: Within
template: blog
---

View File

@ -0,0 +1 @@
# Webapp Abstractions

1
src/15-conclusion.md Normal file
View File

@ -0,0 +1 @@
# Conclusion

View File

@ -1,6 +1,2 @@
--- # Data Types
sitename: Within
title: "Go Training 2: Types"
template: blog
---

View File

@ -1,6 +1,2 @@
--- # Slices, Arrays and Maps
sitename: Within
title: "Go Training 3: Slices, Arrays and Maps"
template: blog
---

View File

@ -1,6 +1,2 @@
--- # Control Structures
sitename: Within
title: "Go Training 4: Control Structures"
template: blog
---

View File

@ -1,5 +1 @@
--- # Functions
sitename: Within
title: "Go Training 5: Functions"
template: blog
---

View File

@ -1,6 +1,2 @@
--- # Structures
sitename: Within
title: "Go Training 6: Structures"
template: blog
---

View File

@ -1,6 +0,0 @@
---
sitename: Within
title: "Go Training 7: HTTP Operations"
template: blog
---

1
src/7-interfaces.md Normal file
View File

@ -0,0 +1 @@
# Interfaces

1
src/8-http.md Normal file
View File

@ -0,0 +1 @@
# HTTP Operations

View File

@ -1,6 +0,0 @@
---
sitename: Within
title: "Go Training 8: Testing"
template: blog
---

View File

@ -1,6 +0,0 @@
---
sitename: Within
title: "Go Training 9: Writing a Command-line App"
template: blog
---

1
src/9-testing.md Normal file
View File

@ -0,0 +1 @@
# Testing

12
src/intro.md Normal file
View File

@ -0,0 +1,12 @@
# Introduction
This book is a simplified view on how to get up and productive with the Go
programming language. It assumes very little about the reader and is intended
for people who are new to programming and want to do more with Go in particular.
Like always, feedback is very welcome. Any feedback I get will be used to help
make this book even better.
Please visit https://christine.website/contact to get in touch with me.
Thanks for reading!