This is a creation in Article, where the information may have evolved or changed.
Go programming language Getting Started Tutorial
Original: http://golang.org
Translation: Liu Jinhu/Liu Yuntao <yuntao.liu#gmail.com> http://www.log4think.com
Directory
-
Introduced
-
Hello, World
-
Compile
-
Echo
-
Data type Types
-
assigning Allocation
-
Constant Constants
-
I/O Package
-
Rotting Cats
-
Sort
-
Print output
-
Prime
-
Multiplexing
Introduced
This document is an introductory introductory tutorial on the basics of Go programming language, preferring readers familiar with C or C + +. This article is not a complete guide to a language, and if necessary, you should look at the "Language Specification" (Language Specification). After reading this tutorial, you may want to continue looking at "effective go", a document that digs deeper into how to use the Go language. There is also a tutorial on getting started on 3rd: first day, second day, third day.
This article will describe some of the key features of the language in a series of appropriate procedures. All of the sample programs are operational (at the time of this writing), and they are submitted to the repository's/doc/progs/directory.
The program fragment labels the line number in the source file, leaving the line number in front of the blank line blank for clarity.
Hello, World
Let's start with the most common way:
Package Main
Import FMT "FMT"//This package implements formatted input and output
The main func () {
Ten FMT. Printf ("Hello, world; orκαλημέρακόσμε; Orこんにちは World/n ");
11}
Each go source file will use the package statement to declare its name. You can also use the features defined in it by importing other packages. This code imports the package FMT to call our old friend--now it starts with uppercase letters, and is preceded by a package name that qualifies--fmt. Printf.
The declaration of a function uses the keyword func, and the entire program starts with the main function in the main package (after initialization).
String constants can contain Unicode characters, with UTF-8 encoding. (In fact, all the Go Program source files are encoded using UTF-8)
Comments are in the same way as C + +:
/* ... */
// ...
Later, we will continue to talk about print.
Compile
Go is a compiled language. There are currently two compilers, with the GCCGO compiler using GCC as the backend, and a series of compilers named according to their applicable architecture, such as the 6g x86 structure for 64 bits, 8g for the 32-bit x86 structure, and so on. These compilers run faster and generate more efficient code than GCCGO. At the time of writing (at the end of 2009) They also had a more robust run-time system, but Gccgo was catching up.
Here's a look at how to compile and run the program. Using 6g is like this.
$6g Helloworld.go # compilation; The middle code is in Helloworld.6
$6l Helloworld.6 # link; Output to 6.out
$6.out
Hello, world; orκαλημέρακόσμε; Orこんにちは World
$
The way Gccgo looks is more traditional.
$ GCCGO Helloworld.go
$ a.out
Hello, world; orκαλημέρακόσμε; Orこんにちは World
$
Echo
The next step is to implement a UNIX traditional command echo:
Package Main
By Import (
"OS";
"Flag"; Command line option Parser
10)
var omitnewline = flag. Bool ("n", False, "don ' t print final newline")
+ Const (
Space = "";
Newline = "/n";
17)
Func Main () {
Flag. Parse (); Scans the arg list and sets up flags
A var s string = "";
For I: = 0; I < flag. Narg (); i++ {
If i > 0 {
s + = Space
25}
-S + = flag. ARG (i);
27}
If!*omitnewline {
+ S + = Newline
30}
Os. Stdout.writestring (s);
32}
This procedure is small, but there are several special points. In the previous example, we saw that you could use Func to declare a function, while the keyword VAR, const, and type (which is not currently used) can also be used for declarations, just like import. Note that we can place declarations of the same class in parentheses, separated by semicolons, such as 第7-10 rows and 第14-17 rows. But it is not necessarily so, for example, to write
Const SPACE = ""
Const Newline = "/n"
Semicolons are not required here. In fact, no semicolon is required after any top-level declaration. But if a series of declarations are made within a single parenthesis, they need to be separated by semicolons.
You can use semicolons as you would in C, C + + or Java, but you can omit semicolons in many cases if you like. Semicolons are used to denote a separation between statements, rather than to indicate their abort. Therefore, for the last statement in a block of code, there are no semicolons available. The semicolon after the curly brace is also optional, just like in the C language. Compared to the source code of ECHO, only the 8th, 15, and 21 lines must be semicolon-delimited, of course, in the 22nd line of the for statement in order to separate the three expressions also need to add a semicolon. The semicolons in lines 9th, 16, 26, and 31 are not required, and the semicolon is only convenient for adding statements later.
This program imports the OS package to access the Stdout variable, and the Stdout type is *os. File. The import statement is actually a declaration: normally (as in the Hello World program), it names an identifier FMT is used to access the member variables of the imported package, and the package is imported from the "FMT" file under the current directory or standard library. In this program, we specify a name for the imported explicitly type, by default, the package name is a name that is already defined in the imported package and usually matches the file name. So in this "Hello World" program, you can write only import "FMT".
You can specify an import name for the package arbitrarily, but it is usually only necessary to resolve the name conflict.
With the OS. Stdout, we can print the string using its WriteString method.
After importing the flag package, line 12th creates a global variable to hold the Echo's-n option flag. The type of the Omitnewline variable is a *bool--pointer to the BOOL value.
Parameter parsing is performed in Main.main and a variable of the local string type is created to construct the output content. The declaration statement is as follows
var s string = "";
Here we use the keyword Var, followed by the variable name and data type, then you can continue to connect = To assign the initial value.
Go tries to keep it as concise as possible, and this statement can also be used in a shorter form. Because the initial value is a constant of a string type, there is no need to declare the data type, so this declaration can be written like this:
var s = "";
Or it can be used directly in a shorter form:
S: = "";
Operator: = In the Go language is often used in the declaration of the initial value, such as the following statement declaration:
For I: = 0; I < flag. Narg (); i++ {
The flag packet parses the command-line arguments and saves the parameter values in a list.
There are several differences between the for statement in the go language and the C language. First, for is the only loop statement, there is no while statement or do statement. Second, the three clauses following the FOR statement do not require parentheses, but curly braces are required. This applies equally to if and switch statements. A few more examples will be shown later to illustrate other uses of the for statement.
The loop body constructs the string s by appending (+ =) flags and spaces. After the loop, if the-n flag is not set, the program appends a blank line and finally outputs the result.
Note that the function Main.main does not return a value. It is defined in this way, if it reaches the end of Main.main, it means "success", and if you want to indicate an error and return, you can call
Os. Exit (1)
The OS package also contains some other common features, such as the OS. Args is used by the flag package to access command-line arguments.
(not to be continued)