设置本地环境

您需要以下两个软件来设置您本地的Go语言的开发环境 -

文本编辑器和Go编译器

文本编辑器

您需要一个文本编辑器来编写您的程序,比如Windows Notepad、OS Edit command、Brief、Epsilon、EMACS、vim、vi等

软件及其版本可能会因为操作系统而有不同,比如Notepad只有在Windows下才支持,而vim和vi可以同事用在Windows、Linux、UNIX操作系统下。

.go

在开始之前,您需要知道如何编写一个程序,保存、编译,最后执行。

Go 编译器

源文件是供人类阅读,它需要被编译,转换为本地机器指令,这样您机器上的CPU才能一条指令一条指令的执行。Go编译器编译源文件,生成最终可执行的程序。

Go编译器的二进制可执行版本有FreeBSD(发布版8或者以上)、Linux、Mac OS X(雪豹或以上)、Windows下32位和64位的x86架构版本。

下面的章节阐述如何在不同的操作系统下二进制安装Go编译器。

压缩包下载

C:\>go
操作系统压缩包的名字
Windowsgo1.4.windows-amd64.msi
Linuxgo1.4.linux-amd64.tar.gz
Macgo1.4.darwin-amd64-osx10.8.pkg
FreeBSDgo1.4.freebsd-amd64.tar.gz
UNIX/Linux/Mac OS X、FreeBSD
/usr/local/usr/local/go
tar -C /usr/local -xzf go1.4.linux-amd64.tar.gz
/usr/local/go/binPATH
操作系统命令
Linuxexport PATH = $PATH:/usr/local/go/bin
Macexport PATH = $PATH:/usr/local/go/bin
FreeBSDexport PATH = $PATH:/usr/local/go/bin

Windows系统下安装

使用如下的步骤进行安装,将MSI安装包安装在C:\Go下,安装程序会将c:\Go\bin的目录添加到Windows的环境变量中。重启命令行工具使配置生效。

验证安装是否有效

test.go

文件: test.go

编译并执行test.go −

输出

Local Environment Setup

If you are still willing to set up your environment for Go programming language, you need the following two software available on your computer −

A text editor
Go compiler

Text Editor

You will require a text editor to type your programs. Examples of text editors include Windows Notepad, OS Edit command, Brief, Epsilon, EMACS, and vim or vi.

The name and version of text editors can vary on different operating systems. For example, Notepad is used on Windows, and vim or vi is used on Windows as well as Linux or UNIX.

The files you create with the text editor are called source files. They contain program source code. The source files for Go programs are typically named with the extension ".go".

Before starting your programming, make sure you have a text editor in place and you have enough experience to write a computer program, save it in a file, compile it, and finally execute it.

The Go Compiler

The source code written in source file is the human readable source for your program. It needs to be compiled and turned into machine language so that your CPU can actually execute the program as per the instructions given. The Go programming language compiler compiles the source code into its final executable program.

Go distribution comes as a binary installable for FreeBSD (release 8 and above), Linux, Mac OS X (Snow Leopard and above), and Windows operating systems with 32-bit (386) and 64-bit (amd64) x86 processor architectures.

The following section explains how to install Go binary distribution on various OS.

Download Go Archive

Download the latest version of Go installable archive file from Go Downloads. The following version is used in this tutorial: go1.4.windows-amd64.msi.

It is copied it into C:>go folder.

操作系统Archive name
Windowsgo1.4.windows-amd64.msi
Linuxgo1.4.linux-amd64.tar.gz
Macgo1.4.darwin-amd64-osx10.8.pkg
FreeBSDgo1.4.freebsd-amd64.tar.gz

Installation on UNIX/Linux/Mac OS X, and FreeBSD

Extract the download archive into the folder /usr/local, creating a Go tree in /usr/local/go. For example −

tar -C /usr/local -xzf go1.4.linux-amd64.tar.gz

Add /usr/local/go/bin to the PATH environment variable.

OSOutput
Linuxexport PATH = $PATH:/usr/local/go/bin
Macexport PATH = $PATH:/usr/local/go/bin
FreeBSDexport PATH = $PATH:/usr/local/go/bin

Installation on Windows

Use the MSI file and follow the prompts to install the Go tools. By default, the installer uses the Go distribution in c:\Go. The installer should set the c:\Go\bin directory in Window's PATH environment variable. Restart any open command prompts for the change to take effect.

Verifying the Installation

Create a go file named test.go in C:\>Go_WorkSpace.

File: test.go

Now run test.go to see the result −

Output