macos本地搭载环境
介绍 (Introduction)
Go is a programming language that was born out of frustration at Google. Developers continually had to pick a language that executed efficiently but took a long time to compile, or to pick a language that was easy to program but ran inefficiently in production. Go was designed to have all three available at the same time: fast compilation, ease of programming, and efficient execution in production.
Go是一种编程语言,它源于Google的无奈之举。 开发人员不断不得不选择一种执行效率高但编译时间长的语言,或者选择一种易于编程但在生产中运行效率低下的语言。 Go旨在同时提供所有三个功能:快速编译,易于编程以及在生产中高效执行。
While Go is a versatile programming language that can be used for many different programming projects, it’s particularly well suited for networking/distributed systems programs, and has earned a reputation as “the language of the cloud.” It focuses on helping the modern programmer do more with a strong set of tooling, removing debates over formatting by making the format part of the language specification, as well as making deployment easy by compiling to a single binary. Go is easy to learn, with a very small set of keywords, which makes it a great choice for beginners and experienced developers alike.
尽管Go是一种通用的编程语言,可以用于许多不同的编程项目,但它特别适合于网络/分布式系统程序,并赢得了“云语言”的美誉。 它着重于帮助现代程序员使用强大的工具集来做更多的事情,通过使格式成为语言规范的一部分来消除有关格式的争论,并通过编译为单个二进制文件使部署变得容易。 Go易于学习,只包含很少的关键字,对于初学者和经验丰富的开发人员而言,它都是不错的选择。
This tutorial will guide you through installing Go on your local macOS machine and setting up a programming workspace via the command line.
本教程将指导您在本地macOS机器上安装Go并通过命令行设置编程工作区。
先决条件 (Prerequisites)
You will need a macOS computer with administrative access that is connected to the internet.
您将需要一台具有管理访问权限的macOS计算机,该计算机已连接到Internet。
第1步-打开终端 (Step 1 — Opening Terminal)
We’ll be completing most of our installation and setup on the command line, which is a non-graphical way to interact with your computer. That is, instead of clicking on buttons, you’ll be typing in text and receiving feedback from your computer through text as well. The command line, also known as a shell, can help you modify and automate many of the tasks you do on a computer every day, and is an essential tool for software developers.
我们将在命令行上完成大部分安装和设置,这是与计算机交互的非图形方式。 也就是说,您将不用输入按钮,而是输入文本,并通过文本接收来自计算机的反馈。 命令行(也称为外壳程序)可以帮助您修改和自动化每天在计算机上执行的许多任务,并且是软件开发人员的必备工具。
The macOS Terminal is an application you can use to access the command line interface. Like any other application, you can find it by going into Finder, navigating to the Applications folder, and then into the Utilities folder. From here, double-click the Terminal like any other application to open it up. Alternatively, you can use Spotlight by holding down the CMD
and SPACE
keys to find Terminal by typing it out in the box that appears.
macOS终端是可用于访问命令行界面的应用程序。 与任何其他应用程序一样,您可以通过以下方法找到它:进入Finder,导航到Applications文件夹,然后进入Utilities文件夹。 从此处,像其他任何应用程序一样,双击终端以将其打开。 或者,您可以通过按住CMD
和SPACE
键来使用Spotlight,方法是在出现的框中键入它以找到终端。
There are many more Terminal commands to learn that can enable you to do more powerful things. The article “An Introduction to the Linux Terminal” can get you better oriented with the Linux Terminal, which is similar to the macOS Terminal.
还有更多的终端命令需要学习,可以使您执行更强大的功能。 文章“ Linux终端简介 ”可以使您更好地了解与MacOS终端类似的Linux终端。
Now that you have opened up Terminal, you can download and install Xcode, a package of developer tools that you will need in order to install Go.
现在,您已经打开Terminal,可以下载并安装Xcode ,这是安装Go所需的开发人员工具包。
第2步-安装Xcode (Step 2 — Installing Xcode)
Xcode is an integrated development environment (IDE) that is comprised of software development tools for macOS. You can check if Xcode is already installed by typing the following in the Terminal window:
Xcode是一个集成开发环境 (IDE),由用于macOS的软件开发工具组成。 您可以通过在“终端”窗口中键入以下内容来检查Xcode是否已安装:
- xcode-select -p xcode选择-p
The following output means that Xcode is installed:
以下输出表示已安装Xcode:
Output
/Library/Developer/CommandLineTools
If you received an error, then in your web browser install Xcode from the App Store and accept the default options.
如果收到错误,请在Web浏览器中从App Store安装Xcode并接受默认选项。
Once Xcode is installed, return to your Terminal window. Next, you’ll need to install Xcode’s separate Command Line Tools app, which you can do by typing:
Xcode安装完成后,返回到“终端”窗口。 接下来,您需要安装Xcode的单独的命令行工具应用程序,您可以通过键入以下命令进行安装:
- xcode-select --install xcode-select-安装
At this point, Xcode and its Command Line Tools app are fully installed, and we are ready to install the package manager Homebrew.
至此,Xcode及其命令行工具应用程序已完全安装,我们准备安装软件包管理器Homebrew。
步骤3 —安装和设置Homebrew (Step 3 — Installing and Setting Up Homebrew)
While the macOS Terminal has a lot of the functionality of Linux Terminals and other Unix systems, it does not ship with a package manager that accommodates best practices. A package manager is a collection of software tools that work to automate installation processes that include initial software installation, upgrading and configuring of software, and removing software as needed. They keep installations in a central location and can maintain all software packages on the system in formats that are commonly used. Homebrew provides macOS with a free and open source software package managing system that simplifies the installation of software on macOS.
尽管macOS终端具有Linux终端和其他Unix系统的许多功能,但它并未附带适用于最佳实践的软件包管理器。 软件包管理器是软件工具的集合,这些工具可用于自动化安装过程,包括初始软件安装,软件的升级和配置以及根据需要删除软件。 它们将安装保持在中央位置,并可以以常用格式维护系统上的所有软件包。 Homebrew为macOS提供了免费的开源软件包管理系统,从而简化了macOS上软件的安装。
To install Homebrew, type this into your Terminal window:
要安装Homebrew,请在“终端”窗口中输入以下内容:
- /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" / usr / bin / ruby -e“ $(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”
Homebrew is made with Ruby, so it will be modifying your computer’s Ruby path. The curl
command pulls a script from the specified URL. This script will explain what it will do and then pauses the process to prompt you to confirm. This provides you with a lot of feedback on what the script is going to be doing to your system and gives you the opportunity to verify the process.
Homebrew是用Ruby制作的,因此它将修改您计算机的Ruby路径。 curl
命令从指定的URL中提取脚本。 该脚本将说明它将执行的操作,然后暂停该过程以提示您进行确认。 这为您提供了有关脚本将对系统执行的操作的大量反馈,并为您提供了验证过程的机会。
If you need to enter your password, note that your keystrokes will not display in the Terminal window but they will be recorded. Simply press the return
key once you’ve entered your password. Otherwise press the letter y
for “yes” whenever you are prompted to confirm the installation.
如果需要输入密码,请注意,您的击键将不会显示在“终端”窗口中,但会被记录下来。 输入密码后,只需按return
键。 否则,每当系统提示您确认安装时,将字母y
表示“是”。
Let’s walk through the flags that are associated with the curl
command:
让我们看一下与curl
命令关联的标志:
The
-f
or--fail
flag tells the Terminal window to give no HTML document output on server errors.-f
或--fail
标志告诉终端窗口在服务器错误时不输出任何HTML文档。The
-s
or--silent
flag mutescurl
so that it does not show the progress meter, and combined with the-S
or--show-error
flag it will ensure thatcurl
shows an error message if it fails.-s
或--silent
标志使curl
静音,以使其不显示进度表,并与-S
或--show-error
标志结合使用,将确保curl
在失败时显示错误消息。The
-L
or--location
flag will tellcurl
to redo the request to a new place if the server reports that the requested page has moved to a different location.如果服务器报告所请求的页面已移至其他位置,则
-L
或--location
标志将告知curl
将请求重做至新位置。
Once the installation process is complete, we’ll put the Homebrew directory at the top of the PATH
environment variable. This will ensure that Homebrew installations will be called over the tools that macOS may select automatically that could run counter to the development environment we’re creating.
安装过程完成后,我们将Homebrew目录放在PATH
环境变量的顶部。 这将确保通过macOS可能会自动选择的工具调用Homebrew安装,这些工具可能会与我们正在创建的开发环境背道而驰。
You should create or open the ~/.bash_profile
file with the command-line text editor nano using the nano
command:
您应该使用nano
命令,使用命令行文本编辑器nano创建或打开~/.bash_profile
文件:
- nano ~/.bash_profile 纳米〜/ .bash_profile
Once the file opens up in the Terminal window, write the following:
在“终端”窗口中打开文件后,编写以下内容:
export PATH=/usr/local/bin:$PATH
To save your changes, hold down the CTRL
key and the letter o
, and when prompted press the RETURN
key. Now you can exit nano by holding the CTRL
key and the letter x
.
要保存更改,请按住CTRL
键和字母o
,然后在出现提示时按RETURN
键。 现在,您可以通过按住CTRL
键和字母x
退出nano。
Activate these changes by executing the following in Terminal:
通过在Terminal中执行以下操作来激活这些更改:
- source ~/.bash_profile 来源〜/ .bash_profile
Once you have done this, the changes you have made to the PATH
environment variable will go into effect.
完成此操作后,对PATH
环境变量所做的更改将生效。
You can make sure that Homebrew was successfully installed by typing:
您可以通过键入以下命令来确保成功安装了Homebrew:
- brew doctor 酿造医生
If no updates are required at this time, the Terminal output will read:
如果此时不需要更新,则终端输出将显示:
Output
Your system is ready to brew.
Otherwise, you may get a warning to run another command such as brew update
to ensure that your installation of Homebrew is up to date.
否则,您可能会收到警告,请运行其他命令(例如brew update
以确保您安装的Homebrew是最新的。
Once Homebrew is ready, you can install Go.
准备好Homebrew之后,即可安装Go。
第4步-安装Go (Step 4 — Installing Go)
You can use Homebrew to search for all available packages with the brew search
command. For the purpose of this tutorial, you will search for Go-related packages or modules:
您可以使用自制软件来搜索与所有可用的软件包brew search
命令。 就本教程而言,您将搜索与Go相关的软件包或模块:
- brew search golang 酿造搜索golang
Note: This tutorial does not use brew search go
as it returns too many results. Because go
is such a small word and would match many packages, it has become common to use golang
as the search term. This is common practice when searching the internet for Go-related articles as well. The term Golang was born from the domain for Go, which is golang.org
.
注意 :本教程不使用brew search go
因为它返回的结果太多。 因为go
是一个很小的词,并且可以匹配许多软件包,所以使用golang
作为搜索词已经很普遍了。 在Internet上搜索与Go相关的文章时,这也是常见的做法。 术语Golang从域去,这是天生golang.org
。
The Terminal will output a list of what you can install:
终端将输出您可以安装的列表:
Output
golang golang-migrate
Go will be among the items on the list. Go ahead and install it:
Go将在列表中。 继续安装:
- brew install golang 酿造安装golang
The Terminal window will give you feedback regarding the installation process of Go. It may take a few minutes before installation is complete.
“终端”窗口将为您提供有关Go安装过程的反馈。 安装完成可能需要几分钟。
To check the version of Go that you installed, type the following:
要检查已安装的Go版本,请键入以下内容:
- go version 去版本
This will output the specific version of Go that is currently installed, which will by default be the most up-to-date, stable version of Go that is available.
这将输出当前安装的Go的特定版本,默认情况下它将是可用的最新,最稳定的Go版本。
In the future, to update Go, you can run the following commands to first update Homebrew and then update Go. You don’t have to do this now, as you just installed the latest version:
将来,要更新Go,可以运行以下命令来首先更新Homebrew,然后再更新Go。 您不必立即执行此操作,因为您已经安装了最新版本:
- brew update 酿造更新
- brew upgrade golang 酿造升级golang
brew update
will update the formulae for Homebrew itself, ensuring you have the latest information for packages you want to install. brew upgrade golang
will update the golang
package to the latest release of the package.
brew update
将更新Homebrew本身的公式,以确保您具有要安装的软件包的最新信息。 brew upgrade golang
会将golang
软件包更新golang
软件包的最新版本。
It is good practice to ensure that your version of Go is up-to-date.
优良作法是确保您的Go版本是最新的。
With Go installed on your computer, you are now ready to create a workspace for your Go projects.
在计算机上安装Go之后,您现在就可以为Go项目创建工作区了。
第5步-创建您的Go工作区 (Step 5 — Creating Your Go Workspace)
Now that you have Xcode, Homebrew, and Go installed, you can go on to create your programming workspace.
现在,您已经安装了Xcode,Homebrew和Go,可以继续创建编程工作区。
The Go workspace will contain two directories at its root:
Go工作空间的根目录将包含两个目录:
src
: The directory that contains Go source files. A source file is a file that you write using the Go programming language. Source files are used by the Go compiler to create an executable binary file.src
:包含Go源文件的目录。 源文件是您使用Go编程语言编写的文件。 Go编译器使用源文件来创建可执行的二进制文件。bin
: The directory that contains executables built and installed by the Go tools. Executables are binary files that run on your system and execute tasks. These are typically the programs compiled by your source code or another downloaded Go source code.bin
:该目录包含由Go工具构建和安装的可执行文件。 可执行文件是在系统上运行并执行任务的二进制文件。 这些通常是由您的源代码或其他已下载的Go源代码编译的程序。
The src
subdirectory may contain multiple version control repositories (such as Git, Mercurial, and Bazaar). You will see directories like github.com
or golang.org
when your program imports third party libraries. If you are using a code repository like github.com
, you will also put your projects and source files under that directory. This allows for a canonical import of code in your project. Canonical imports are imports that reference a fully qualified package, such as github.com/digitalocean/godo
.
src
子目录可能包含多个版本控制存储库(例如Git , Mercurial和Bazaar )。 你会看到这样的目录github.com
或golang.org
当你的程序导入第三方库。 如果您使用的是github.com
类的代码存储库,还将把项目和源文件放在该目录下。 这允许在项目中规范地导入代码。 规范导入是引用完全合格的软件包的导入,例如github.com/digitalocean/godo
。
Here is what a typical workspace may look like:
这是典型的工作空间可能看起来像:
.
├── bin
│ ├── buffalo # command executable
│ ├── dlv # command executable
│ └── packr # command executable
└── src
└── github.com
└── digitalocean
└── godo
├── .git # Git reposistory metadata
├── account.go # package source
├── account_test.go # test source
├── ...
├── timestamp.go
├── timestamp_test.go
└── util
├── droplet.go
└── droplet_test.go
The default directory for the Go workspace as of 1.8 is your user’s home directory with a go
subdirectory, or $HOME/go
. If you are using a version of Go earlier than 1.8, it is considered best practice to still use the $HOME/go
location for your workspace.
从1.8开始,Go工作区的默认目录是用户的主目录,其中带有go
子目录,即$HOME/go
。 如果您使用的Go版本早于1.8,则最好的做法是仍将$HOME/go
位置用于工作区。
Issue the following command to create the directory structure for your Go workspace:
发出以下命令为您的Go工作区创建目录结构:
- mkdir -p $HOME/go/{bin,src} mkdir -p $ HOME / go / {bin,src}
The -p
option tells mkdir
to create all parents
in the directory, even if they don’t currently exist. Using {bin,src}
creates a set of arguments to mkdir
and tells it to create both the bin
directory and the src
directory.
-p
选项告诉mkdir
在目录中创建所有parents
,即使它们当前不存在也是如此。 使用{bin,src}
为mkdir
创建一组参数,并告诉它创建bin
目录和src
目录。
This will ensure the following directory structure is now in place:
这将确保以下目录结构到位:
└── $HOME
└── go
├── bin
└── src
Prior to Go 1.8, it was required to set a local environment variable called $GOPATH
. While it is no longer explicitly required to do so, it is still considered a good practice as many third party tools still depend on this variable being set.
在Go 1.8之前,需要设置一个名为$GOPATH
的局部环境变量。 尽管不再明确要求这样做,但由于许多第三方工具仍依赖于此变量的设置,因此仍被视为一种好的做法。
You can set your $GOPATH
by adding it to your ~/.bash_profile
.
您可以通过将$GOPATH
添加到~/.bash_profile
来设置它。
First, open ~/.bash_profile
with nano
or your preferred text editor:
首先,使用nano
或您喜欢的文本编辑器打开~/.bash_profile
:
- nano ~/.bash_profile 纳米〜/ .bash_profile
Set your $GOPATH
by adding the following to the file:
通过将以下内容添加到文件中来设置$GOPATH
:
export GOPATH=$HOME/go
When Go compiles and installs tools, it will put them in the $GOPATH/bin
directory. For convenience, it’s common to add the workspace’s /bin
subdirectory to your PATH
in your ~/.bash_profile
:
当Go编译并安装工具时,它将放置在$GOPATH/bin
目录中。 为了方便起见,通常将工作区的/bin
子目录添加到~/.bash_profile
PATH
中:
export PATH=$PATH:$GOPATH/bin
You should now have the following entries in your ~/.bash_profile
:
现在,您的~/.bash_profile
应该包含以下条目:
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
This will now allow you to run any programs you compile or download via the Go tools anywhere on your system.
现在,这将允许您运行通过系统上任何位置的Go工具编译或下载的任何程序。
To update your shell, issue the following command to load the global variables you just created:
要更新您的shell,请发出以下命令以加载刚创建的全局变量:
- . ~/.bash_profile 。 〜/ .bash_profile
You can verify your $PATH
is updated by using the echo
command and inspecting the output:
您可以通过使用echo
命令并检查输出来验证$PATH
是否已更新:
- echo $PATH 回声$ PATH
You should see your $GOPATH/bin
which will show up in your home directory. If you were logged in as sammy
, you would see /Users/sammy/go/bin
in the path.
您应该看到$GOPATH/bin
,它将显示在主目录中。 如果以sammy
身份登录,则路径中将显示/Users/sammy/go/bin
。
Output
/Users/sammy/go/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
Now that you have the root of the workspace created and your $GOPATH
environment variable set, you will create your future projects with the following directory structure. This example assumes you are using github.com as your repository:
现在,您已经创建了工作空间的根目录并设置了$GOPATH
环境变量, $GOPATH
将使用以下目录结构创建将来的项目。 此示例假定您使用github.com作为存储库:
$GOPATH/src/github.com/username/project
If you were working on the https://github.com/digitalocean/godo project, you would put it in the following directory:
如果您正在处理https://github.com/digitalocean/godo项目,则将其放在以下目录中:
$GOPATH/src/github.com/digitalocean/godo
Structuring your projects in this manner will make projects available with the go get
tool. It will also help readability later.
以这种方式构建项目将使go get
工具可以使用项目。 这也将有助于以后的可读性。
You can verify this by using the go get
command to fetch the godo
library:
您可以使用go get
命令来获取godo
库,以进行验证:
- go get github.com/digitalocean/godo 去github.com/digitalocean/godo
We can see it successfully downloaded the godo
package by listing the directory:
通过列出以下目录,我们可以看到它成功下载了godo
软件包:
- ls -l $GOPATH/src/github.com/digitalocean/godo ls -l $ GOPATH / src / github.com / digitalocean / godo
You will receive output similar to this:
您将收到类似于以下的输出:
Output
-rw-r--r-- 1 sammy staff 2892 Apr 5 15:56 CHANGELOG.md
-rw-r--r-- 1 sammy staff 1851 Apr 5 15:56 CONTRIBUTING.md
.
.
.
-rw-r--r-- 1 sammy staff 4893 Apr 5 15:56 vpcs.go
-rw-r--r-- 1 sammy staff 4091 Apr 5 15:56 vpcs_test.go
In this step, you created a Go workspace and configured the necessary environment variables. In the next step you will test the workspace with some code.
在此步骤中,您创建了Go工作区并配置了必要的环境变量。 在下一步中,您将使用一些代码来测试工作区。
第6步-创建一个简单程序 (Step 6 — Creating a Simple Program)
Now that you have your Go workspace set up, it’s time to create a simple “Hello, World!” program. This will make sure that your workspace is working and gives you the opportunity to become more familiar with Go.
现在您已经设置好Go工作区,现在该创建一个简单的“ Hello,World!”了。 程序。 这将确保您的工作区正常工作,并为您提供对Go更加熟悉的机会。
Because you are creating a single Go source file, and not an actual project, you don’t need to be in your workspace to do this.
因为您正在创建一个Go源文件,而不是一个实际的项目,所以您不需要在工作区中执行此操作。
From your home directory, open up a command-line text editor, such as nano
, and create a new file:
在主目录中,打开一个命令行文本编辑器,例如nano
,并创建一个新文件:
- nano hello.go nano hello.go
Once the text file opens up in Terminal, type out your program:
在终端中打开文本文件后,输入程序:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Exit nano by typing the control
and x
keys, and when prompted to save the file press y
.
通过输入control
和x
键退出nano,然后在提示您保存文件时,按y
。
This code will use the fmt
package and call the Println
function with Hello, World!
as the argument. This will cause the phrase Hello, World!
to print out to the terminal when the program is run.
该代码将使用fmt
包,并使用Hello, World!
调用Println
函数Hello, World!
作为论点。 这将导致短语Hello, World!
在程序运行时打印到终端。
Once you exit out of nano
and return to your shell, run the program:
一旦退出nano
并返回外壳,请运行程序:
- go run hello.go 去跑hello.go
The hello.go
program that you just created will cause Terminal to produce the following output:
您刚刚创建的hello.go
程序将使Terminal产生以下输出:
Output
Hello, World!
In this step, you used a basic program to verify that your Go workspace is properly configured.
在此步骤中,您使用了一个基本程序来验证Go工作区是否已正确配置。
结论 (Conclusion)
Congratulations! At this point you have a Go programming workspace set up on your local macOS machine and can begin a coding project!
恭喜你! 至此,您已经在本地macOS机器上设置了Go编程工作区,并且可以开始编码项目!
macos本地搭载环境