golang go语言
Go (or golang) is a programming language created at Google in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson. It is a compiled, statically-typed language in the tradition of Algol and C.
Go (或golang )是由Robert Griesemer,Rob Pike和Ken Thompson于2007年在Google创建的一种编程语言。 它是Algol和C的传统汇编语言,是静态类型的语言。
Go has garbage collection, limited structural typing, memory safety, and CSP-style concurrent programming features added. The compiler and other language tools originally developed by Google are all free and open source.
Go具有垃圾回收,有限的结构类型输入,内存安全性和CSP风格的并发编程功能。 由Google最初开发的编译器和其他语言工具都是免费和开源的。
Go's popularity is increasing fast. It is a great choice for building web applications.
Go的受欢迎程度正在Swift提高。 这是构建Web应用程序的绝佳选择。
For more information head to Go’s Homepage. Want a quick tour of Go? Check out the docs here.
有关更多信息,请访问Go的主页 。 想快速浏览Go吗? 在此处查看文档。
Now let's see how to install and get started with Go.
现在,让我们看看如何安装和开始使用Go。
安装 (Installation)
使用Homebrew安装Golang: (Install Golang with Homebrew:)
$ brew update
$ brew install golang
使用tarball在MacOS上安装Go (Installing Go on MacOS using a tarball)
链接到tarball (Link to tarball)
You can get the link to the MacOS tarball archive from the Latest Stable section of the golang download page.
您可以从golang下载页面的Latest Stable部分中获得指向MacOS tarball存档的链接。
安装过程 (Installation Process)
In this installation process we’ll use the latest stable version as of this writing (go 1.9.1). For a newer or older version simply replace the link in the first step. Check the golang download page to see what versions are currently available.
在此安装过程中,我们将使用撰写本文时的最新稳定版本(版本1.9.1)。 对于较新或较旧的版本,只需在第一步中替换链接即可。 检查golang下载页面以查看当前可用的版本。
安装Go 1.9.1 (Installing Go 1.9.1)
$ curl -O https://storage.googleapis.com/golang/go1.9.1.darwin-amd64.tar.gz
$ sudo tar -C /usr/local -xzf go1.9.1.darwin-amd64.tar.gz
$ export PATH=$PATH:/usr/local/go/bin
使用apt在Ubuntu上安装Golang (Install Golang on Ubuntu with apt)
Using Ubuntu’s Source Package Manager (apt) is one of the easiest ways to install Go. You won’t get the latest stable version, but for the purpose of learning this should be enough.
使用Ubuntu的Source Package Manager(apt)是安装Go的最简单方法之一。 您不会获得最新的稳定版本,但是出于学习目的,这已经足够了。
$ sudo apt-get update
$ sudo apt-get install golang-go
检查Go的安装和版本 (Check the installation and version of Go)
To check if go was successfully installed, run:
要检查go是否已成功安装,请运行:
$ go version
> go version go1.9.1 linux/amd64
This will print the version of Go that's installed to the console. If you see a version of Go, you'll know that the installation went smoothly.
这将打印已安装到控制台的Go版本。 如果您看到Go的版本,就会知道安装过程顺利。
设置工作区 (Setup the workspace)
添加环境变量: (Add Environment variables:)
First, you’ll need to tell Go the location of your workspace.
首先,您需要告诉Go工作区的位置。
We’ll add some environment variables into shell config. One of does files located at your home directory bash_profile, bashrc or .zshrc (for Oh My Zsh Army)
我们将一些环境变量添加到shell配置中。 位于您的主目录bash_profile,bashrc或.zshrc中的一个文件之一(适用于Oh My Zsh Army)
$ vi .bashrc
then add those lines to export the required variables
然后添加这些行以导出所需的变量
这实际上是您的.bashrc文件 (This is actually your .bashrc file)
export GOPATH=$HOME/go-workspace # don't forget to change your path correctly!
export GOROOT=/usr/local/opt/go/libexec
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin
创建您的工作区 (Create your workspace)
创建工作空间目录树: (Create the workspace directories tree:)
$ mkdir -p $GOPATH $GOPATH/src $GOPATH/pkg $GOPATH/bin
$GOPATH/src : Where your Go projects / programs are located
$GOPATH/pkg : contains every package objects
$GOPATH/bin : The compiled binaries home
快速开始 (Quickstart)
For a quickstart and boilerplate Go project, try Alloy.
对于快速入门和样板Go项目,请尝试Alloy 。
- Clone Alloy repository 克隆合金存储库
git clone https://github.com/olliecoleman/alloy
cd alloy
2. Install the dependencies
2.安装依赖项
glide install
npm install
3. Start the development server
3.启动开发服务器
go install
alloy dev
http://localhost:1212
http://localhost:1212
Alloy uses Node, NPM, and Webpack.
Alloy使用Node,NPM和Webpack。
高朗游乐场 (The Golang Playground)
Learning how to install Go on your local machine is important. But if want to start playing with Go right in your browser, then Go Playground is the perfect sandbox to get started right away!
了解如何在本地计算机上安装Go很重要。 但是,如果要立即在浏览器中开始使用Go进行播放,那么Go Playground是立即开始使用的理想沙箱!
Just open a new browser window and go to the Playground.
只需打开一个新的浏览器窗口,然后转到Playground 。
Once there you’ll get the buttons:
到达那里后,您将获得以下按钮:
- Run 跑
- Format 格式
- Imports 进口货
- Share 分享
The Run button just sends the instructions for compiling the code you wrote to the Google servers that run the Golang back end.
“ 运行”按钮仅将用于编译您编写的代码的指令发送到运行Golang后端的Google服务器。
The Format button implements the idiomatic formatting style of the language. You can read more about it here.
格式按钮实现该语言的惯用格式样式。 您可以在此处了解更多信息。
Imports just check what packages you have declared within import(). An import path is a string that uniquely identifies a package. A package’s import path corresponds to its location inside a workspace or in a remote repository (explained below). You can read more here.
导入仅检查您在import()中声明了哪些包。 导入路径是唯一标识软件包的字符串。 包的导入路径与其在工作空间内或在远程存储库中的位置相对应(如下所述)。 您可以在这里阅读更多内容。
With Share you get a URL where the code you just wrote is saved. This is useful when asking for help showing your code.
使用“ 共享”,您将获得一个保存刚编写的代码的URL。 在寻求帮助显示代码时,此功能很有用。
You can take a more in-depth Tour of Go here and learn more about the playground in the article Inside the Go Playground.
您可以在此处进行更深入的围棋之旅,并在文章围棋场内了解有关操场的更多信息。
去地图 (Go Maps)
A map, called a dictionary in other languages, “maps” keys to values. A map is declared like this:
映射(在其他语言中称为字典)将值的映射“键”。 映射声明如下:
var m map[Key]Value
make
make
m = make(map[Key]Value)
Anything can be used as a key or as a value.
任何东西都可以用作键或值。
修改地图 (Modifying maps)
Here are some common action with maps.
以下是一些关于地图的常见操作。
插入/更改元素 (Inserting/Changing elements)
foom
mfoo
m["foo"] = bar
获取元素 (Getting elements)
foom
mfoo
element = m["foo"]
删除元素 (Deleting elements)
foom
mfoo
delete(m, "foo")
检查是否已使用密钥 (Check if a key has been used)
foom
foom
element, ok = m["foo"]
oktrueelementm["foo"]okfalseelement
oktrueelementm["foo"]okfalseelement
地图文字 (Map literals)
You can directly create map literals:
您可以直接创建地图文字:
var m = map[string]bool{
"Go": true,
"JavaScript":false,
}
m["Go"] // true
m["JavaScript"] = true // Set Javascript to true
delete(m, "JavaScript") // Delete "JavaScript" key and value
language, ok = m["C++"] // ok is false, language is bool's zero-value (false)
有关Go的更多信息: (More info about Go:)
golang go语言