Local Environment settings
Here we describe setting up the Go programming language environment, which requires the following two software on your computer, (A) text editor and (B) going compiler.
Text Editor
This will be used to type the program. Examples of some editors include Windows Notepad, os edit commands, Jane, Brief, Epsilon, Emacs, and Vim or VI.
The text editor name and version may not be the same on different operating systems. For example, Notepad will be used for Windows;vim or VI to be used on Windows and Linux or UNIX.
The files created by the editor are called source files and include program source code. The source file for the Go program is usually named extension to ". Go".
Before you start programming, make sure you have a text editor in place, and that you have enough experience to write a computer program, save it in a file, compile it, and execute it eventually.
Go compiler
the source code written in sources is the human-readable source of the program. It needs to be "compiled" and converted into machine language so that the CPU can actually execute the program according to the given instruction.
The Go programming language compiler will be used to compile the source code to the final executable program. Here we assume you have a basic knowledge of programming language compilers.
The go release is installed in binary FreeBSD (8 and above), in Linux,mac os X (Snow Leopard and above) and Windows 32-bit (386) and 64-bit (AMD64) X86 processor architecture operating system.
The following sections will guide you through how to install the binary distribution versions of Go on a variety of operating systems.
Download Go archive
Download the latest version of Go installation archive: Go download. While writing this tutorial, download the Go1.4.windows-amd64.msi and copy it to the C:\>go directory.
OS Archive Name
installed in Unix/linux/mac OS x and FreeBSD
unzip the download archive to/usr/local, i.e. create a go tree in/usr/local/go. For example:
Tar-c/usr/local-xzf go1.4.linux-amd64.tar.gz
Installing on Windows
use the MSI file and follow the prompts to install the Weiqi tool. By default, Setup uses the go to distribution in C:\Go. Setup should set the C:\Go\bin directory to the Windows PATH environment variable. Restart any open command prompt for the change to take effect.
Verifying installation
Create a Go file named Test.go c:\>go_workspace
File:test.go
Copy Code code as follows:
Package Main
Import "FMT"
Func Main () {
Fmt. Println ("Hello, world!")
}
Now run Test.go see the results as follows:
C:\go_workspace>go Run Test.go
Validating output
As we learn the basic building blocks of the Go programming language, let's look at a minimalist go program structure so that we can use it as a reference for upcoming chapters.
Go Hello World Example
The Go program consists mainly of the following parts:
- Package Declaration
- Import Package
- Function
- Variable
- Statements and expressions
- Comments
Let's take a look at the simple "Hello World" code that will print the word:
Copy Code code as follows:
Package Main
Import "FMT"
Func Main () {
/* This is the My-i-A-sample program. */
Fmt. Println ("Hello, world!")
}
Let's take a look at the various parts of the program above:
- The first line of the package body defines the package name. This is a package that must be declared to run on the Go program. The main package is the starting point to run the program. Each package has a path and a name associated with it.
- The next line of import "FMT" is a preprocessing command that tells the compiler to include files in the package fmt.
- The next line func main () is the beginning of the execution of the program.
- The next line of/*...*/is ignored by the compiler, and it has been added to the program to add comments. Therefore, such a line is called a comment in a program. Comments are also used//similar to Java or C + + annotations.
- Next line FMT. Println (.....) ) is to provide another function to make the message "Hello, world!" To be displayed on the screen. Here the FMT package has been exported and the Println method is used to display the message on the screen.
Pay attention to the Println method. In the Go language, a name is exported if it starts with a capital letter. Export refers to a function or variable/frequently accessed to the appropriate package import.
To execute the GO program:
Let's look at how to save the source code file and how to compile and run it. The following are simple steps:
Open the Text editor and add the above code
Save File as Hello.go
Open a command prompt and go to the directory to save the file.
Type go run Hello.go and enter to run your code.
If there are no errors in your code, you can see the words "Hello world" on the screen.
Make sure that the go compiler contains the source file Hello.go in the path and directory and runs it.