# Fundamentals of Go
![](img/witch-learning.png)
Hello, and welcome to learning Go! It's great to have you here.
## What's this?
This is a set of introductory exercises in Go programming, suitable for complete beginners. If you don't know anything about Go yet, or programming, but would like to learn, you're in the right place!
(If you do already know something about Go, you should find these exercises relatively straightforward, but still worth doing.)
## Is there a book to accompany these exercises?
Absolutely. [For the Love of Go](https://bitfieldconsulting.com/books/love) guides you through every step of the process, from installing Go to explaining all the ins and outs of test-driven development. It also contains almost all the code you'll need to solve these exercises, with full details of how it works.
[![](img/cover_small.png)](https://bitfieldconsulting.com/books/love)
You don't need to buy the book to work through these exercises, though it'll definitely help! But if you do buy it, you'll be helping support me and my work, including developing new exercises and books, and mentoring people from non-traditional backgrounds to help them into tech and software development careers. Which would be amazing! You can also [sponsor me on GitHub](https://github.com/sponsors/bitfield).
Here it is:
* [For the Love of Go](https://bitfieldconsulting.com/books/love)
There's also a video course to accompany the book:
* [For the Love of Go Video Course](https://bitfieldconsulting.com/courses/love)
## What you'll learn
In this set of exercises, you'll learn:
* How to run tests for a Go program
* How to automatically format your Go code correctly
* The basic pattern that all Go tests should follow
* How to declare and import Go _packages_ (units of code)
* How to design multiple _test cases_ and use them in your tests
* How to test error handling in your programs
* A simple, reliable, test-first development workflow
## How to do the exercises
Each exercise is described in a separate section of this README file. Each section briefly explains what the exercise is about, and outlines what you'll be doing. There'll be one or more goals for you to achieve, marked with **GOAL:** in bold. (Don't worry if you're not sure how to do it. There'll usually be some helpful suggestions following the goal description.)
If you're finding the goals easy, or want a bit more of a challenge, there are some _stretch goals_, too. These are optional; if you're not interested or not ready to tackle them, just skip over them.
So let's get started!
# 1: Testing times
![](img/calculator.jpg)
It's your first day as a Go developer at Texio Instronics, a major manufacturer of widgets and what-nots, and you've been assigned to the Future Projects division to work on a very exciting new product: a Go-powered electronic calculator! Welcome aboard.
In this exercise, all you need to do is make sure your Go environment is set up and everything is working right.
One of your new colleagues has already made a start on the calculator project. She's placed a Go package in the [`calculator.go`](calculator.go) file, and an accompanying test in [`calculator_test.go`](calculator_test.go). You're going to run this test and make sure it passes.
**GOAL:** Successfully run the test.
**STRETCH GOAL:** Use your editor's Go support features to run the test directly in the editor. For example, in Visual Studio Code, there's a little 'Run test' link above each test function, and a 'Run package tests' link at the top of the file. Find out how to do this in the editor you're using.
# 2: Getting in shape
![](img/hiking.png)
The next thing to do is ensure that your Go code is formatted correctly. All Go code is formatted in a standard way, using a tool called `gofmt` (pronounced, in case you were wondering, 'go-fumpt'). In this exercise you'll see how to use it to check and reformat your code (if necessary).
The file `calculator.go` in this directory contains a deliberate formatting mistake, just to make sure you're on your toes!
**GOAL:** Run `gofmt` to check the formatting and find out what is wrong.
**GOAL:** Use `gofmt` to fix the formatting automatically.
**STRETCH GOAL:** Experiment with `gofmt`. Try formatting your code in different ways and see what changes `gofmt` makes to it. It's interesting to note what it does and doesn't change.
# 3: Fixing a hole
![](img/chained.png)
Now that your development environment is all set up, your colleague needs your help. She has been working on getting the calculator to subtract numbers, but there's a problem: the test is not passing. Can you help?
You'll find the test code commented out in the `calculator_test.go` file:
```go
// func TestSubtract(t *testing.T) {
// t.Parallel()
// var want float64 = 2
// got := calculator.Subtract(4, 2)
// if want != got {
// t.Errorf("want %f, got %f", want, got)
// }
// }
```
**GOAL:** Get this test passing!
**STRETCH GOAL:** Find out what other ways there are to fail a test using the `testing.T`. What other methods does it have? Do any of them look potentially useful?
# 4: Go forth and multiply
![](img/rocket.png)
Excellent work! You now have a calculator which can add and (correctly) subtract. That's a great start. Let's turn to multiplication now.
Up to now you've been running existing tests and modifying existing code. For the first time you're going to write the test, and the function it's testing!
**GOAL:** Write a test for a function `Multiply()` which, just like the `Add()` and `Subtract()` functions, takes two numbers as parameters, and returns a single number representing the result.
**GOAL:** Write the minimum code necessary to get the test to compile and fail.
**GOAL:** Write the minimum code necessary to get `TestMultiply` to pass.
Why did we say "the minimum code necessary"? Because, in a sense, the test itself defines what `Multiply` needs to do. Once it does that, and the test passes, we should _stop coding_.
**GOAL:** Make `TestMultiply` fail deliberately.
There's one final step required for us to be really sure that both the test and the code are correct. We need to see the test fail again. To do this, change the expected return value from `Multiply` to something that you know is wrong, and run `go test` again.
**STRETCH GOAL:** Think about other ways this test could be wrong. Try some of them out. What happens? What could you do to catch such potential problems?
Nice work! Go on to the next section.
# 5: Testing to destruction
![](img/crash-dummy.png)
Now that you've successfully designed and implemented a new calculator feature, test-first, it's time to think a little more about testing, and how you can extend it. The `Add`, `Subtract`, and `Multiply` functions are so simple that it's fairly difficult (though not at all impossible) to get them wrong. If `TestAdd` passes for one set of inputs, for example, it's hard to imagine other inputs that would make it fail.
But this doesn't apply to more complicated functions, where it's quite likely that they could work correctly for some inputs, but not for others. In this situation it will be a good idea to test multiple _cases_ for each function: that is to say, different inputs with different expected results.
**GOAL:** Define a struct type suitable for expressing test cases for your calculator functions. It should include the test inputs, and the expected output.
**GOAL:** Declare a slice of test cases for `TestAdd`, using your struct type, and rewrite the test function to loop over them, testing each case in turn.
**GOAL:** Rewrite `TestSubtract`, and `TestMultiply` to test multiple cases for each function, too. Try to think of interesting and varied test cases: for example, negative numbers, zeroes, fractional numbers.
As before, once each test is passing, break it deliberately and make sure it fails in the expected way, with a helpful failure message.
**STRETCH GOAL:** Add a string field `name` to y
没有合适的资源?快使用搜索试试~ 我知道了~
Go基础知识练习,与JohnArundel的“FortheLoveofGo”一书配套。___下载.zip
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
共16个文件
png:8个
go:2个
md:1个
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 127 浏览量
2023-04-18
00:13:05
上传
评论
收藏 658KB ZIP 举报
温馨提示
Go基础知识练习,与JohnArundel的“FortheLoveofGo”一书配套。___下载.zip
资源推荐
资源详情
资源评论
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![004](https://img-home.csdnimg.cn/images/20210720083646.png)
![002](https://img-home.csdnimg.cn/images/20210720083646.png)
![003](https://img-home.csdnimg.cn/images/20210720083646.png)
![005](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 16 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
![avatar-vip](https://csdnimg.cn/release/downloadcmsfe/public/img/user-vip.1c89f3c5.png)
快撑死的鱼
- 粉丝: 1w+
- 资源: 9153
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)