跟讀練習: Build a Microservice with Go #1 - Getting Started - 通過影片學習英語口語

選擇練習模式

每種模式都會開啟全屏練習室。

204 句
如果句子過短或過長,請點選 Edit 進行調整。
1
All right then gang, so in this series you're going to learn how to make a microservice with Go
2
and a framework called Qi or Kai or however else you want to pronounce it.
3
And to teach you how to do this I've brought in
4
the YouTuber Dreams of Code who's got a bunch of really nice videos about Go, Python, Rust and other subjects as well.
5
His videos are all really slick and informative so definitely check out his channel.
6
The link is down below the video.
7
Anyway without further ado So let the tutorial begin.
8
Go is a modern programming language that prides itself on being simple to use whilst remaining powerful and performant.
9
Because of this, Go is a language that is popular for building many different types of software, such as command line tools, network applications and microservices.
10
In this series, we're going to walk through building an API microservice in Go from start to finish.
11
By the end, you'll learn how to spin up a basic microservice API, which connects to a Redis database and is responsible for creating, reading, updating and deleting our data type.
12
So without further ado, let's get started.
13
Before we can begin writing code, we'll need to make sure we have Go installed on our system.
14
I'll be using macOS for the majority of this video, but it should be similar in other operating systems as well.
15
If you head on over to the Golang website, you should be able to find installation instructions for your operating system.
16
If you're okay with a basic installation, I recommend following the instructions found here.
17
For me however, I'm going to use a tool called Goenv, which allows us to manage multiple versions of Go on the same system.
18
So I'm going to show you how to get that set up.
19
To install Goenv, you can either follow the instructions on GitHub or use your operating systems package manager, if you have one of course.
20
For macOS, it's as simple as using brew install goenv.
21
Once Goenv is installed, run the following line to ensure that it loads every time your terminal starts, and then restart your terminal window.
22
You can test that everything is working okay with the following command.
23
If this doesn't work, then you may have missed a step.
24
With everything set up, we're now ready to install Go.
25
What's nice about using Goenv is that you're able to install the same version as I am in this tutorial, as it's very likely that a more recent version of Go is currently available at the time you're watching this.
26
You're probably okay to use the latest version of Go as the developers try hard to remain backwards compatible.
27
However, there's never a guarantee of that being the case.
28
So feel free to use the same version as I am in the video if the most recent one doesn't work.
29
To install Go, type in the following command.
30
This will install version 1.20.5.
31
After that, we can run the following command to set the global version of Go in Goenv.
32
Finally, you can test that everything is working correctly by running the go version command, which should print out 1.20.5.
33
Now we're ready to start building.
34
By the way, in case you haven't noticed, I pretty much use the terminal for most of my development.
35
I also use NeoVim as my editor of choice, but you may feel more comfortable using something like VS Code or another graphical editor.
36
The CLI commands that I use in this video should work the same for you, however, and so you may need to jump into the terminal at certain points.
37
First things first, we'll need a directory to store our project.
38
I'm going to create mine under projects slash orders API.
39
Feel free to choose a different directory for yourself however.
40
Once inside we now need to actually initialize our go project.
41
We can do this by running go mod init.
42
You'll need to add the project repo as the final argument.
43
This can be any git repo, such as github or gitlab.
44
Here I'm specifying my github repository.
45
Additionally this repo doesn't need to exist at this stage, you can always create it later and you can always rename this if you need to.
46
GoMod is basically Go's dependency management solution, similar to Node's npm or Ruby's gems.
47
However, unlike those systems, it's actually decentralized and works off of git, and therefore each Go project is specified by the git repo it relates to.
48
After running GoMod in it, you should see a go.mod file inside of your project.
49
If we open this up, we can actually see that this has our project repo inside of it.
50
Simple stuff.
51
Next, it's a good idea to initialize Git so
52
that we can actually have good hygiene on our project and track changes as we go through.
53
So, just run the git init command to initialize our repository.
54
After that, go ahead and add our initial commit, which should just be our GoMod.
55
Once that's done, open up your project in your editor, and let's start our first file.
56
Let's call this main.go to specify this is where the main function will live.
57
The first line you want to add is the package name, in this case it's the main package.
58
Every file in Go belongs to a package, which is how imports are organised.
59
In this case we're specifying that the main.go file lives in the main package, which tells the compiler that our code is a program and so it builds an executable.
60
Next we need to add our main function.
61
Every Go program has a main function, which acts as the main entry point to our code.
62
As is customary, let's start off by printing hello world inside of our main function.
63
To do this we first need to import another package, called fmt, or format.
64
Some others might call it phmt, but I disagree.
65
To do this we need to call the import format command just underneath our main package.
66
If you have a modern editor configured properly, imports should be automatically imported for you, but it's worthwhile us doing this manually just to see how this works.
67
Next head back to the main function and type in format.println .
68
You'll notice that this function is camelCase but the first letter is a capital.
69
Go uses this for encapsulation to specify that the function is public.
70
Now our program is complete.
71
Let's jump over to a terminal window and actually run this code.
72
We can do so by using go, run, and to the name of our file path.
73
In this case, main.go.
74
After a short period of time, you should see Hello World print to the console.
75
Perfect.
76
We've just created our first Go program.
77
Congratulations.
78
We can also build our code by using the go build command, converting it into a binary.
79
Go is pretty awesome and you can build binaries for other architectures if you like.
80
However, that's beyond the scope of this video.
81
Now that we have the basics down, let's turn it up and actually build a simple web server, which we'll use as the starting point for our API.
82
Go comes with a rich standard library and provides all we need to get a basic web server running, which we can extend later on.
83
To start, first add the net slash HTTP package to our imports.
84
This package provides functionality for creating both HTTP clients and servers, which is what we want.
85
Now we can instantiate an HTTP server using the following line in our main function.
86
This creates a basic go type of HTTP server and stores it in a variable called server.
87
You'll notice we're using an ampersand, which means we're storing this as a pointer and taking the memory address, rather than the value.
88
this type we can then define some of the properties of our server.
89
The first is the server's address, which will bind to port 3000 using the given string.
90
Next we'll need to specify a handler for our server.
91
This is an interface that will be called when our server receives a request.
92
Let's use the http handler function call and specify a function named basic handler, which we're going to create now.
93
To do so let's define a new function using the func keyword.
94
We'll call this basic handler as we suggested and specify the following parameters, which is what we need for our function to be able to conform to their HTTP handler interface.
95
The first parameter is called w and is a HTTP response writer.
96
This is an interface type that enables us to write what will eventually be our HTTP response.
97
The second type is called r and is a pointer of type HTTP request.
98
We can tell this is a pointer by the use of the asterisks.
99
This type represents the inbound HTTP request the server receives from the client.
100
Inside of the function let's go ahead and write a body response of hello world.
101
We can do this by calling the write method of the response writer.
102
This function expects a byte array so we'll need to cast our string to be one, which is done by using the syntax on screen.
103
All that remains is to actually run our server.
104
We can do this by calling the listen and serve method on our server type.
105
This method returns an error and it's good practice in Go to handle them.
106
This is done by using an if error check, which will check if the error exists, i.e. it's not nil.
107
Inside of the block we can then handle the error, which in our case we're just going to log it to stdout using the format.println method.
108
Okay, now we're able to go ahead and test out this code.
109
Let's jump over to a new terminal window.
110
We can run our app using the go run command we saw earlier.
111
Once that's done we can open up another terminal window and run a curl command to our localhost at port 3000.
112
This will send an HTTP GET request to our server.
113
And we can see we get a response of hello world which shows us our server is working as expected.
114
Very nice.
115
To stop the the server, head over to the window where we're running it and press Ctrl and C.
116
Now that we have our basic web server up, it's time to add some additional functionality to it.
117
The first thing we're going to want to do is handle HTTP routing.
118
You may have noticed but we currently only have a single handler in our project, which will handle all of the inbound requests.
119
We could do some basic routing inside of this handler, but that can be incredibly tedious to do so, and it's a challenge to properly handle things such as path parameters.
120
So whilst Go does have a robust standard library, it's worthwhile for us to use a third-party dependency at this point.
121
But which one?
122
Well, my go-to router used to be GorillaMux, which offered a great deal of functionality for very little abstraction,
123
whilst remaining true to the HTTP handler interface of the standard library.
124
However, as of the end of 2022, GorillaMux has been archived as read-only, So for future proofing our API, it's probably not the best choice.
125
There's plenty of other options out there however, but for this video I'm going to go with Qi.
126
This router is very similar to GorillaMarks and also conforms to the http interface of the standard library, so it works as a suitable replacement in my opinion.
127
To use Qi we first need to add it to our code.
128
There's a couple of ways to do this and they all involve using GoMod.
129
Before I show you these approaches, now is a good time to commit our code so we can easily roll back any changes.
130
If we run git status we can see that our API binary currently appears in the untracked files.
131
That's not ideal.
132
When using git it's a bad practice to commit any build
133
artifacts as they can add junk to a git repo pretty quickly.
134
To solve this we need to add a .gitignore.
135
Let's go ahead and create one.
136
Inside we can then specify the name of any file or folders we want git to, well, ignore.
137
Let's add the name of our binary, which is API.
138
Okay great, now if we run git status again we should see that the API is no longer available to be tracked.
139
Also because we're on macOS it's probably worthwhile to also add the .ds store files to our git ignore as well.
140
These files are created whenever you navigate to a directory using finder.
141
Rather than opening up our .git ignore file again, we can actually run the following command, which will concatenate a string into our .gitignore file.
142
Once that's done, go ahead and run the git add command to add all of the files in our directory, and then run the git commit command to store them.
143
Now we can demonstrate the two approaches for adding a dependency to our project.
144
The first approach is to use the go get command, which will download and add an entry to the go mod of our current go project, provided we're in one.
145
Let's try this by adding chi to our project.
146
To do so, type in the following command.
147
You'll notice that we're specifying the git repo of qi.
148
Remember, GoMod is decentralized, and so it uses the git repo URL as the package identifier.
149
Now if we take a look at our go.mod, we can see that an entry has been added.
150
So this is the first approach.
151
However, the second approach tends to be the one I use more often.
152
Let's reset our changes to show how we would use this use this.
153
For this approach, we just import the package into our code like we would any other.
154
Then when we want to add it to our GoMod, we just call GoMod tidy in our terminal.
155
This will tell Go to search for any packages within our project and add them to the Go.mod.
156
As you can see, Qi has now been added.
157
Also, you may have noticed that a Go.sum file now exists as well.
158
This is used to ensure consistency across package versions in case they are updated upstream.
159
It's a good idea to check in both the GoMod and the Go.sum files into our our Git repo, so we'll do this now.
160
Okay, with Qi added to our project and our code committed, let's actually use it with our HTTP server.
161
Heading back to our main function, we need to first initialise a new instance of the Qi router for use with our server.
162
We can do this by calling Qi.newRouter and assigning it to a variable called router.
163
In Go, any method prefix with the word new is conventionally a constructor.
164
Oftentimes it's preferable to use the constructor method instead of directly instantiating an instance, as they will often initialise private properties of the type.
165
With our new router created, let's go ahead and add a root for get slash hello, and we'll match it to the basic handler that we've already got defined.
166
We can do this by calling the router.get method and passing in our path as the first parameter.
167
The second parameter requires an http.handler function, which we can just set to our basic handler.
168
The last thing we need to do is add our router to the server handler.
169
What's really nice about Qi is that the router type itself conforms to the HTTP handler interface, so all we have to do here is replace our handler with our router instead.
170
With that done, we can go ahead and test our behaviour.
171
To do so, let's rerun our app using gorun on our main.go file.
172
Heading over to another terminal window, we can then run our curl command against our localhost at port 3000.
173
However this time you'll notice we get a 404 page not found response.
174
That's because we haven't set any handlers for the slash root, and she is automatically handling that for us, which is pretty neat.
175
Let's run curl again, but this time point it to our slash hello path.
176
This time we get back the expected hello world response.
177
There's one last thing for us to test.
178
Let's try this with a POST request instead of a GET request.
179
We can do this with curl using the X flag, which allows us to specify the HTTP method.
180
Make sure you set X to uppercase.
181
We'll also add the lowercase v flag so that we're able to see the full HTTP headers.
182
Here you can see we're getting an HTTP status response of 405, which stands for Method Not Allowed.
183
This is because we only specified a route for the method of GET in our path, and therefore Qi provides this error for us.
184
With a basic router added, it's clear we're missing something.
185
And that something is logging.
186
Qi fortunately provides us a logging middleware that we're able to use right out of the box.
187
We can add this to our router by first importing the middleware package from the Qi plugin by using the following line.
188
With our middleware imported we can then add it to the router via the use method.
189
Now if we restart our server, we should start to see logs appear whenever we handle a request and response.
190
Nice.
191
Now that we're in a good spot, it's time to commit our code again.
192
To do so, go ahead and run the git add command to stage our changes, and then type in the git commit command to go ahead and commit them.
193
By the way, the M flag stands for message, in case you hadn't guessed that already.
194
This allows us to specify the message we want to store along with our actual commit.
195
By the way, if you want to watch this entire course now without YouTube adverts, you can do.
196
It's all up on the NetNinja website, netninja.dev.
197
You can buy the course for $2 to get instant access to all of it, or you can sign up to NetNinja Pro and get instant access to all of my courses without adverts,
198
as well as premium courses not found on YouTube including my Udemy ones that's nine dollars a month
199
and you can get your first month half price
200
when you use this promo code right here
201
so I'm going to leave this link down below in the video description for you to sign up
202
and I really hope you enjoy this series
203
and please do not forget to share subscribe and like the videos
204
that really helps a lot and I'm going to see you in the very next lesson

下載應用

流利口語所需的一切

AI 發音評分逐句打分
音標練習掌握每個音
詞彙學習建立你的詞庫
詞彙遊戲邊玩邊學

同頻道

FOR YOU

推薦影片

本課的詞彙與口語要點

這節 C1 級別的口語課以影片“Build a Microservice with Go - Getting Started”為素材。 影片中反覆出現的詞有:add, server, http, git, command。 這段影片共有 204 個句子、3192 個單詞可供跟讀。 講話部分時長為 16:16。 說話人語速較快,每分鐘約 196 個詞,會出現較多連讀和弱讀。 83% 的單詞屬於英語最常用的 3,000 詞,其餘的詞建議在練習前先學一下。

影片中的重點詞彙

影片中最難的 15 個單詞,附發音和釋義:

單詞發音釋義
router 名詞/ˈɹaʊ.tɚ/路由器, 路徑器 /路徑器
initialize 動詞/ɪˈnɪʃəlaɪz/初始化
parameter 名詞/pəˈɹæm.ə.tɚ/引數 /引數, 參量 /參量
directory 名詞/dɪˈɹɛktəɹi/名錄 /名錄, 簿
binary 形容詞/ˈbaɪ.nə.ɹi/二進 /二進
conform 動詞/kənˈfɔɹm/遵從
pointer 名詞/ˈpɔɪn.təɹ/教鞭
functionality 名詞功能, 功能性
worthwhile 形容詞/ˌwɜː(ɹ)θˈwaɪl/值得, 划算
inbound 形容詞/ˈɪnbaʊnd/入境的, 入站的
repository 名詞/ɹɪˈpɒz.ɪ.t(ə.)ɹɪ/倉庫 /倉庫
tutorial 名詞/ˌtjuːˈtɔːɹɪəl/輔導 /輔導, 教程
ampersand 名詞/ˈæm.pə(ɹ).sænd/和號 /和號
artifact 名詞/ˈɑɹtɪfækt/人工製品 /人工製品
compiler 名詞/kəmˈpaɪlɚ/編者

影片中出現的短語動詞

單詞發音釋義
go ahead 動詞/ˌɡoʊ əˈhɛd/請便 /請便
check out 動詞退房
go through 動詞通過 /通過
sign up 動詞報名 /報名, 註冊 /註冊

值得反覆跟讀的句子

影片中簡短而完整的句子,可以直接用在日常對話裡:

  • Here I'm specifying my github repository.
  • Let's jump over to a new terminal window.
  • Let's go ahead and create one.
  • Let's try this by adding chi to our project.
  • There's one last thing for us to test.

影片中的語法

說話人最常用的結構,並附上影片中的原話:

結構影片中的用法
被動語態 be + 過去分詞 — 強調發生了什麼,而不是誰做的is installed · is specified · are organised
定語從句 who / which + 從句 — 補充說明人或事物package, which is · servers, which is · ampersand, which means
現在完成時 have/has + 過去分詞 — 過去發生但與現在仍有關聯的事I've brought · haven't noticed · We've just created

需要注意的發音

說話人用了 56 次縮略和弱讀形式,例如 we're, we'll, I'm。請按聽到的簡短形式來說。

  • “th” 音: worthwhile /ˌwɜː(ɹ)θˈwaɪl/, underneath /ˌʌndɚˈniθ/
  • “sh” 和 “zh” 音: initialize /ɪˈnɪʃəlaɪz/, instantiate /ɪnˈstænʃieɪt/, abstraction /æbˈstɹæk.ʃn̩/, conventionally /kənˈvɛnʃənəli/
  • 長單詞——注意重音位置: initialize /ɪˈnɪʃəlaɪz/, parameter /pəˈɹæm.ə.tɚ/, directory /dɪˈɹɛktəɹi/, dependency /dɪˈpɛndənsi/, decentralize /diːˈsɛntɹəlaɪz/

中文母語者容易讀錯的音:

  • /v/ — 不要讀成 /w/,上齒輕觸下唇: advert /ædvˈɝt/, conventionally /kənˈvɛnʃənəli/, informative /ɪnˈfɔːmətɪv/, navigate /ˈnæv.ɪ.ɡeɪt/, archive /ˈɑɹˌkaɪv/
  • 詞尾子音 — 要讀清楚,後面不要加母音: initialize /ɪˈnɪʃəlaɪz/, advert /ædvˈɝt/, decentralize /diːˈsɛntɹəlaɪz/, inbound /ˈɪnbaʊnd/, instantiate /ɪnˈstænʃieɪt/
  • /r/ 和 /l/ 的區別: curl /kɜɹl/, worthwhile /ˌwɜː(ɹ)θˈwaɪl/, decentralize /diːˈsɛntɹəlaɪz/, tutorial /ˌtjuːˈtɔːɹɪəl/, graphical /ˈɡɹæfɪkəl/

如何用這段影片練習

  1. 先完整聽一遍影片,不要開口,記下不認識的單詞。
  2. 先用 0.75 倍速逐句跟讀,熟練之後再回到正常速度。
  3. 錄下自己的聲音並與原聲對比,特別注意 router, initialize, parameter 這類單詞。

什麼是跟讀法?

跟讀法 (Shadowing) 是一種有科學依據的語言學習技巧,最初開發用於專業口譯員的培訓,並由多語言者Alexander Arguelles博士普及。這個方法簡單而強大:您在聽英語母語原聲的同時立即大聲重複——就像是一個延遲1-2秒緊跟說話者的影子。與被動聽力或語法練習不同,跟讀法強迫您的大腦和口腔肌肉同時處理並模模擬實的講話模式。研究表明它能顯著提高發音準確性,語調,節奏,連讀,聽力理解和口語流利度——使其成為雅思口語備考和真實英語交流最有效的方法之一。

影子跟讀法: 閱讀完整分步指南 →