Week 9: Go
Go was originally created by people who were not too fond of the C++ programming language. Go is a strongly typed and compiled language, which aims mostly at being both easy to learn and fast to execute. Go is also one of the few languages to have a fanta
- PDF / 765,884 Bytes
- 19 Pages / 439.37 x 666.142 pts Page_size
- 10 Downloads / 204 Views
Week 9: Go I have nine armchairs from which I can be critical. —Rick Moranis Go was originally created by people who were not too fond of the C++ programming language. Go is a strongly typed and compiled language, which aims mostly at being both easy to learn and fast to execute. Go is also one of the few languages to have a fantastic logo! The logo, shown in Figure 9-1, was designed by Renée French, and I understand that I can use it here, as she is credited.
Figure 9-1. Gopher, Renée French’s logo for the Go language
© Nicolas Modrzyk 2019 N. Modrzyk, Building Telegram Bots, https://doi.org/10.1007/978-1-4842-4197-4_9
181
Chapter 9
Week 9: Go
In this chapter, I will review how to install the Go binary, followed by first steps and basic Go samples, before moving on to writing a Telegram bot and, finally, writing a command-line binary to send different Telegram objects via the API.
Installation of Go To download Go, you can use the prepackaged version from the Go web site, located at https://golang.org/dl/. Most platforms have an option available for download, as shown in Figure 9-2.
Figure 9-2. Go packages for your preferred platform Your favorite package manager should also have the Go package available. # On Linux/Manjaro -S go # on macOS brew install go # on Windows choco install golang Once installed, you should check with the version subcommand whether you have a relatively current version.
182
Chapter 9
Week 9: Go
# Current version $ go version go version go1.11 linux/amd64 It is usually recommended that you create a GOPATH variable, which is used so that your Go packages can be downloaded and stored in a known place. export GOPATH=$HOME/go Visual Studio Code has a plug-in for Go, and to install it, it is recommended that you follow this recipe. There are multiple plug-ins for Go, but the one from Microsoft is very solid and is shown in Figure 9-3.
Figure 9-3. The Go plug-in for Visual Studio Code The Go-related tasks.json for the Visual Studio Code build tasks, and its Command+Shift+B shortcut, required to execute code from within Visual Studio Code, is written following, for convenience. { "version": "2.0.0", "tasks": 183
Chapter 9
Week 9: Go
[ { "label": "letsgo", "command": "go", "args": [ "run", "${file}" ], "options": { "cwd": "${workspaceRoot}" }, "group": { "kind": "build", "isDefault": true } } ] } From the tasks.json file, you’ll notice that the command to build a Go program is go run. Now, let’s move on to the first Go program.
Let’s Go The basic structure of a program in Go is separated into three main blocks.
184
•
The package definition, done with package
•
The imports, all defined in one block
•
The main function that gets executed when running the go run command
Chapter 9
Week 9: Go
We’ll start our Go adventures with a program that will read
Data Loading...