# 開始使用Go
| [Prerequisites](https://golang.org/doc/tutorial/getting-started#prerequisites)[Install Go](https://golang.org/doc/tutorial/getting-started#install)[Write some code](https://golang.org/doc/tutorial/getting-started#code)[Call code in an external package](https://golang.org/doc/tutorial/getting-started#call)[Write more code](https://golang.org/doc/tutorial/getting-started#write-more) | |
| ------------------------------------------------------------ | ---- |
| | |
In this tutorial, you'll get a brief introduction to Go programming. Along the way, you will:
- Install Go (if you haven't already).
- Write some simple "Hello, world" code.
- Use the `go` command to run your code.
- Use the Go package discovery tool to find packages you can use in your own code.
- Call functions of an external module.
**Note:** For other tutorials, see [Tutorials](https://golang.org/doc/tutorial/index.html).
## Prerequisites
- **Some programming experience.** The code here is pretty simple, but it helps to know something about functions.
- **A tool to edit your code.** Any text editor you have will work fine. Most text editors have good support for Go. The most popular are VSCode (free), GoLand (paid), and Vim (free).
- **A command terminal.** Go works well using any terminal on Linux and Mac, and on PowerShell or cmd in Windows.
## Install Go
Just use the [Download and install](https://golang.org/doc/install) steps.
## Write some code
Get started with Hello, World.
1. Open a command prompt and cd to your home directory.
On Linux or Mac:
```
cd
```
On Windows:
```
cd %HOMEPATH%
```
2. Create a hello directory for your first Go source code.
For example, use the following commands:
```
mkdir hello
cd hello
```
3. In your text editor, create a file hello.go in which to write your code.
4. Paste the following code into your hello.go file and save the file.
```
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
```
This is your Go code. In this code, you:
- Declare a `main` package (a package is a way to group functions).
- Import the popular [`fmt` package](https://golang.org/pkg/fmt/), which contains functions for formatting text, including printing to the console. This package is one of the [standard library](https://golang.org/pkg/) packages you got when you installed Go.
- Implement a `main` function to print a message to the console. A `main` function executes by default when you run code in the file.
5. Run your code to see the greeting.
```
$ go run hello.go
Hello, World!
```
The [`go run` command](https://golang.org/cmd/go/#hdr-Compile_and_run_Go_program) is one of many `go` commands you'll use to get things done with Go. Use the following command to get a list of the others:
```
$ go help
```
## Call code in an external package
When you need your code to do something that might have been implemented by someone else, you can look for a package that has functions you can use in your code.
1. Make your printed message a little more interesting with a function from an external module.
1. Visit pkg.go.dev and [search for a "quote" package](https://pkg.go.dev/search?q=quote).
2. Locate and click the `rsc.io/quote` package in search results (if you see `rsc.io/quote/v3`, ignore it for now).
3. On the **Doc** tab, under **Index**, note the list of functions you can call from your code. You'll use the `Go` function.
4. At the top of this page, note that package `quote` is included in the `rsc.io/quote` module.
You can use the pkg.go.dev site to find published modules whose packages have functions you can use in your own code. Packages are published in modules -- like `rsc.io/quote` -- where others can use them. Modules are improved with new versions over time, and you can upgrade your code to use the improved versions.
2. In your Go code, import the
```
rsc.io/quote
```
package and add a call to its
```
Go
```
function.
After adding the highlighted lines, your code should include the following:
```
package main
import "fmt"
import "rsc.io/quote"
func main() {
fmt.Println(quote.Go())
}
```
3. Put your own code in a module for tracking dependencies.
When your code imports packages from another module, a go.mod file lists the specific modules and versions providing those packages. That file stays with your code, including in your source code repository.
To create a go.mod file, run the [`go mod init` command](https://golang.org/cmd/go/#hdr-Initialize_new_module_in_current_directory), giving it the name of the module your code will be in (here, just use "hello"):
```
$ go mod init hello
go: creating new go.mod: module hello
```
4. Run your code to see the message generated by the function you're calling.
```
$ go run hello.go
go: finding module for package rsc.io/quote
go: found rsc.io/quote in rsc.io/quote v1.5.2
Don't communicate by sharing memory, share memory by communicating.
```
Notice that your code calls the `Go` function, printing a clever message about communication.
But before it ran the code, `go run` located and downloaded the `rsc.io/quote` module that contains the package you imported. By default, it downloaded the latest version -- v1.5.2. Go build commands are designed to locate the modules required for packages you import.
## Write more code
With this quick introduction, you got Go installed and learned some of the basics. To write some more code with another tutorial, take a look at [Create a Go module](https://golang.org/doc/tutorial/create-module.html).
- 關于本書
- 引言
- 準備工作
- 安裝 Go語言開發環境
- 開始使用Go
- 創建一個Go模塊
- 第一章 手把手系列
- 1.1 教你搭建Nginx教程
- 1.2 教你搭建Jupyter教程
- 1.3 教你搭建Node教程
- 1.4 教你搭建Fabric教程
- 1.5 教你搭建Ethereum教程
- 1.6 教你搭建Bitcoin教程
- 1.7 教你搭建Systemd教程
- 第二章 架構師之路
- 2.1 微服務開發筆記
- 2.2 Docker開發筆記
- 2.3 ElasticSearch開發筆記
- 2.4 Linux開發筆記
- 2.5 Mysql開發筆記
- 2.6 Nginx開發筆記
- 2.7 Redis開發筆記
- 第三章 區塊鏈教程
- 3.1 Bitcoin開發筆記
- 3.2 Ethereum開發筆記
- 3.3 USDT開發筆記
- 第四章 網絡知識庫
- 4.1 比特幣白皮書
- 4.2 以太坊白皮書
- 第五章 技術博客園
- 5.1 Fabric架構詳解
- 5.2 技術開發指南
- 5.3 共識機制詳解
- 第六章 項目管理
- 6.1 項目運行環境
- 6.2 項目經理的角色
- 6.3 第6、7、8章框架
- 第七章 公務員考公
- 7.1 程序員成功上岸經歷
- 7.2 程序員備考的最佳實踐
- 7.3 程序員備考過程中會遇到哪些問題?
- 7.4 公考公平嗎,35歲再去考可以么?
- 7.5 資料、工具推薦和擴展閱讀
- 結論
- 附錄