From c4e8df367c9ab9805462ce04f7178bb33e3e1617 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Mon, 28 Oct 2019 22:38:30 +0000 Subject: [PATCH] fixes --- build.sh | 22 +- src/1-hello-world.md | 226 +----------------- src/10-cli-app.md | 2 + src/10-repo-hygiene.md | 6 - src/11-go-modules.md | 6 - src/11-repo-hygiene.md | 1 + src/12-go-modules.md | 1 + src/12-wiki.md | 6 - src/13-webapp-abstractions.md | 6 - src/13-wiki.md | 1 + src/14-conclusion.md | 5 - src/14-webapp-abstractions.md | 1 + src/15-conclusion.md | 1 + src/2-data-tyes.md | 6 +- src/3-slices-maps.md | 6 +- src/4-control-structures.md | 6 +- src/5-functions.md | 6 +- src/6-structures.md | 6 +- src/7-http.md | 6 - src/7-interfaces.md | 1 + src/8-http.md | 1 + src/8-testing.md | 6 - src/9-cli-app.md | 6 - src/9-testing.md | 1 + ...15-battlesnake.md => bonus-battlesnake.md} | 0 src/intro.md | 12 + 26 files changed, 51 insertions(+), 296 deletions(-) create mode 100644 src/10-cli-app.md delete mode 100644 src/10-repo-hygiene.md delete mode 100644 src/11-go-modules.md create mode 100644 src/11-repo-hygiene.md create mode 100644 src/12-go-modules.md delete mode 100644 src/12-wiki.md delete mode 100644 src/13-webapp-abstractions.md create mode 100644 src/13-wiki.md delete mode 100644 src/14-conclusion.md create mode 100644 src/14-webapp-abstractions.md create mode 100644 src/15-conclusion.md delete mode 100644 src/7-http.md create mode 100644 src/7-interfaces.md create mode 100644 src/8-http.md delete mode 100644 src/8-testing.md delete mode 100644 src/9-cli-app.md create mode 100644 src/9-testing.md rename src/{15-battlesnake.md => bonus-battlesnake.md} (100%) create mode 100644 src/intro.md diff --git a/build.sh b/build.sh index 4ab208b..77e1175 100755 --- a/build.sh +++ b/build.sh @@ -3,25 +3,27 @@ files=' src/title.md CHANGELOG.md + src/intro.md src/1-hello-world.md src/2-data-tyes.md src/3-slices-maps.md src/4-control-structures.md src/5-functions.md src/6-structures.md - src/7-http.md - src/8-testing.md - src/9-cli-app.md - src/10-repo-hygiene.md - src/11-go-modules.md - src/12-wiki.md - src/13-webapp-abstractions.md - src/14-conclusion.md + sec/7-interfaces.md + src/8-http.md + src/9-testing.md + src/10-cli-app.md + src/11-repo-hygiene.md + src/12-go-modules.md + src/13-wiki.md + src/14-webapp-abstractions.md + src/15-conclusion.md src/15-battlesnake.md src/GReeTZ.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 diff --git a/src/1-hello-world.md b/src/1-hello-world.md index b18b759..80c83a3 100644 --- a/src/1-hello-world.md +++ b/src/1-hello-world.md @@ -1,18 +1,12 @@ ---- -sitename: Within -title: "Go Training 1: Get Going" -template: blog ---- +# Hello, World -This post is the first in a series I'm creating to help people learn the -[Go][go] programming language. It's aimed at people who understand the high -level concepts of programming, but haven't had much practical experience with -it. +In this chapter we will cover how to install Go and write a simple "Hello, +world!" program. -[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 -make this course even better. +- The Go programming language compiler installed +- A working "Hello, world!" program written ## 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 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 -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: +That should do it for this chapter. Here are a few optional exercises to try: -```console -$ go mod init your-name.localhost/hello -go: creating new go.mod: module christine-dodrill.localhost/hello -``` - -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. +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. diff --git a/src/10-cli-app.md b/src/10-cli-app.md new file mode 100644 index 0000000..d8e423e --- /dev/null +++ b/src/10-cli-app.md @@ -0,0 +1,2 @@ +# Writing a Command-Line Application + diff --git a/src/10-repo-hygiene.md b/src/10-repo-hygiene.md deleted file mode 100644 index 8700dbf..0000000 --- a/src/10-repo-hygiene.md +++ /dev/null @@ -1,6 +0,0 @@ ---- -sitename: Within -title: "Go Training 10: Repo Hygiene" -template: blog ---- - diff --git a/src/11-go-modules.md b/src/11-go-modules.md deleted file mode 100644 index 77c65ae..0000000 --- a/src/11-go-modules.md +++ /dev/null @@ -1,6 +0,0 @@ ---- -sitename: Within -title: "Go Training 11: Go Modules" -template: blog ---- - diff --git a/src/11-repo-hygiene.md b/src/11-repo-hygiene.md new file mode 100644 index 0000000..2acf471 --- /dev/null +++ b/src/11-repo-hygiene.md @@ -0,0 +1 @@ +# Repo Hygiene diff --git a/src/12-go-modules.md b/src/12-go-modules.md new file mode 100644 index 0000000..f450cda --- /dev/null +++ b/src/12-go-modules.md @@ -0,0 +1 @@ +# Go Modules diff --git a/src/12-wiki.md b/src/12-wiki.md deleted file mode 100644 index 3b54d8c..0000000 --- a/src/12-wiki.md +++ /dev/null @@ -1,6 +0,0 @@ ---- -sitename: Within -title: "Go Training 12: Putting it All Together with a Wiki" -template: blog ---- - diff --git a/src/13-webapp-abstractions.md b/src/13-webapp-abstractions.md deleted file mode 100644 index b1118f3..0000000 --- a/src/13-webapp-abstractions.md +++ /dev/null @@ -1,6 +0,0 @@ ---- -sitename: Within -title: "Go Training 13: Webapp Abstractions" -template: blog ---- - diff --git a/src/13-wiki.md b/src/13-wiki.md new file mode 100644 index 0000000..79b2dca --- /dev/null +++ b/src/13-wiki.md @@ -0,0 +1 @@ +# Putting it All Together with a Wiki diff --git a/src/14-conclusion.md b/src/14-conclusion.md deleted file mode 100644 index e13d355..0000000 --- a/src/14-conclusion.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -title: "Go Training 14: Conclusion" -sitename: Within -template: blog ---- diff --git a/src/14-webapp-abstractions.md b/src/14-webapp-abstractions.md new file mode 100644 index 0000000..43c4c37 --- /dev/null +++ b/src/14-webapp-abstractions.md @@ -0,0 +1 @@ +# Webapp Abstractions diff --git a/src/15-conclusion.md b/src/15-conclusion.md new file mode 100644 index 0000000..2f6b6e3 --- /dev/null +++ b/src/15-conclusion.md @@ -0,0 +1 @@ +# Conclusion diff --git a/src/2-data-tyes.md b/src/2-data-tyes.md index 9a06477..a3b7b87 100644 --- a/src/2-data-tyes.md +++ b/src/2-data-tyes.md @@ -1,6 +1,2 @@ ---- -sitename: Within -title: "Go Training 2: Types" -template: blog ---- +# Data Types diff --git a/src/3-slices-maps.md b/src/3-slices-maps.md index 7064eb2..8554cb9 100644 --- a/src/3-slices-maps.md +++ b/src/3-slices-maps.md @@ -1,6 +1,2 @@ ---- -sitename: Within -title: "Go Training 3: Slices, Arrays and Maps" -template: blog ---- +# Slices, Arrays and Maps diff --git a/src/4-control-structures.md b/src/4-control-structures.md index f7d9c49..5ca1098 100644 --- a/src/4-control-structures.md +++ b/src/4-control-structures.md @@ -1,6 +1,2 @@ ---- -sitename: Within -title: "Go Training 4: Control Structures" -template: blog ---- +# Control Structures diff --git a/src/5-functions.md b/src/5-functions.md index 29993ad..0c5faf5 100644 --- a/src/5-functions.md +++ b/src/5-functions.md @@ -1,5 +1 @@ ---- -sitename: Within -title: "Go Training 5: Functions" -template: blog ---- +# Functions diff --git a/src/6-structures.md b/src/6-structures.md index 0a5a223..5d8ff84 100644 --- a/src/6-structures.md +++ b/src/6-structures.md @@ -1,6 +1,2 @@ ---- -sitename: Within -title: "Go Training 6: Structures" -template: blog ---- +# Structures diff --git a/src/7-http.md b/src/7-http.md deleted file mode 100644 index 4f774c9..0000000 --- a/src/7-http.md +++ /dev/null @@ -1,6 +0,0 @@ ---- -sitename: Within -title: "Go Training 7: HTTP Operations" -template: blog ---- - diff --git a/src/7-interfaces.md b/src/7-interfaces.md new file mode 100644 index 0000000..89ce18f --- /dev/null +++ b/src/7-interfaces.md @@ -0,0 +1 @@ +# Interfaces diff --git a/src/8-http.md b/src/8-http.md new file mode 100644 index 0000000..e12bc54 --- /dev/null +++ b/src/8-http.md @@ -0,0 +1 @@ +# HTTP Operations diff --git a/src/8-testing.md b/src/8-testing.md deleted file mode 100644 index 7cc0a17..0000000 --- a/src/8-testing.md +++ /dev/null @@ -1,6 +0,0 @@ ---- -sitename: Within -title: "Go Training 8: Testing" -template: blog ---- - diff --git a/src/9-cli-app.md b/src/9-cli-app.md deleted file mode 100644 index 813b005..0000000 --- a/src/9-cli-app.md +++ /dev/null @@ -1,6 +0,0 @@ ---- -sitename: Within -title: "Go Training 9: Writing a Command-line App" -template: blog ---- - diff --git a/src/9-testing.md b/src/9-testing.md new file mode 100644 index 0000000..f00b526 --- /dev/null +++ b/src/9-testing.md @@ -0,0 +1 @@ +# Testing diff --git a/src/15-battlesnake.md b/src/bonus-battlesnake.md similarity index 100% rename from src/15-battlesnake.md rename to src/bonus-battlesnake.md diff --git a/src/intro.md b/src/intro.md new file mode 100644 index 0000000..361fcc7 --- /dev/null +++ b/src/intro.md @@ -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!