This is a creation in Article, where the information may have evolved or changed.
1.GO Language development Environment construction
1.1 Compiling the environment installation
Click the link http://code.google.com/p/go/downloads/list
Choose a version, I downloaded the Go1.0.3.windows-386.msi
Double-click Install, choose Install a home directory: for example, C:\Go, there is no need to configure anything.
1.2 Integrated development Environment IDE installation configuration
Open Link: http://code.google.com/p/golangide/downloads/list
Download a version: such as liteidex17.windows.7z
After the download is complete, unzip it in a directory
Click Liteide.exe to open the development interface.
Configuring Environment variables
menu: View → options , select Liteenv, configure environment variables, have three files, system.env,win32.env,win32-user-env need configuration, configuration is simple, only need to modify the previous installation of the compilation Environment directory, Three files are the same.
Goroot=c:\go
Gobin=c:\go\bin
Cancel the # before Paht, the others don't have to change
Select view → set Gopath, refresh to see the system Gopath, browse custom Gopath, set the space directory where the code is stored, the development environment is built
2.helloworld!
The first words of the words "say" after "Born" are, Hello world!
Using the go language, that's what it says:
1 Package Main 2 3 Import (4 "FMT"5)67func main () {8 FMT. Println ("Hello world!" )9 }
Compile and run to appear: Hello world!
3. Go language
The new language developed by the go language Google. Has the following characteristics
Automatic garbage collection, richer built-in data types, function multiple return values, error handling, anonymous functions and closures, types and interfaces, concurrent programming, reflection, multi-language mixed programming
1 Package Main 2 3 // 4import "FMT"5 6func main () { 7 FMT. Println ("Hello, world! ") 8
3.1 Code parsing
Package Main
The start of each go source file declares the package that the go code belongs to, the package is the most basic distribution unit in the Go language, to generate an executable program, you must create a package named Main, and include in the package a function called Main (), which is the starting point for the go executable.
Func Main ()
Main function main (), which is the starting point for executing a program, cannot take arguments, and cannot define a return value. Command the parameters passed in the OS. Save in the args variable. If you need support for command-line switches, use the flag package.
Import Guide Package
Additional packages required to import the program. Because the system function println () is used, the FMT package that the function belongs to is imported.
Attention
You cannot bring in a package that is not in the source code file, or the Go compiler will report a compilation error
Func keyword
All go functions, including the type member functions that are mentioned in object programming, begin with the keyword func. A general function is defined as follows:
1 func function name (parameter list) (return value list) {2 // function Body 3 }
You can see the difference between the go language and other language-defined functions, which also lists the list of return values.
Examples of correspondence:
1 int , value2 float64) (Result Float64,err error) {2 // function Body 3 }
Go supports multiple return values. The above function compute () returns two values, one called result, one is err, not all return values must be assigned, if the return value without definite assignment is set to the default value, the return value is initialized according to the base data type, float64 is 0.0, and error is nil , that is, result is 0.0 err to nil
Comments:
There are two types of code comments in the Go program: block comments and Line comments
/*
Block annotations
*/
Line Comment
Statement End Symbol
About the statement end symbol: The go program does not require the developer to add a semicolon after each statement to indicate the end of the statement, of course, but also can, but Google provides IDE development tools with a semicolon;
Curly brace Position
In Java programming there are references to how curly braces are placed. In the Go language, Jinzhao the opening brace of the function body after the return value of the function, not another line, which is consistent with Java's usual usage, and one row of the left parenthesis of the function body of the same level, with the closing parenthesis in a column, which is not possible in the go language.
1 func function name (parameter list) (return value list) 2 {3 // function Body 4 }
In fact, you can find that the left parenthesis is placed behind the return value list is a good place is: in many editors, such as Notepad, it put the left parenthesis and the right parenthesis between the line together, appears to be a whole, if the left parenthesis is a single line, will appear function definition when the Func line is not with the function body, And putting the left brace on the Func line will appear to seamless the entire function.
3.2 Several commands
View Go version
Cmd--go version output go version go1.0.3
Compiling the source program
Go Build Helloworld.go
Run the post-compilation code
HelloWorld.exe
Unit Tests for functions
Xxx.go corresponding unit test is xxx_test.go, which is the naming convention in Go engineering
2. Sequential programming
2.1 Variables
Fundamentally, a variable is the name of a piece of data storage space, a program can request a chunk of data storage space by defining a variable, and then use that storage space by invoking (referencing) the variable name.
2.1.1 Variable definition
The way of variable declaration of Go language is quite different from other languages. Introduces a keyword VAR (but not required) that represents a variable
Definition syntax
var variable name variable type
Example: var name string
This way in fact more in line with English habits can be defined above the way to expand into English: The variable name is a string variable name is a string, if you remove the verb, quantifiers can be seen var name string Compare the English grammatical structure that we usually use.
In a similar way:
1var v1int2 var v2 string3VAR v3 [10]int //defines a fixed-length array4var V4 []int //defines a variable-length array5 var v5 struct{6Fint7 }8var V6 *int //Pointers9var V7 map[string]int //dictionary, or associative array similar to Java's map, key is of type string, value is int typeTenvar V8 func (Aint)int
Variable block
You can use the VAR keyword to declare several keywords to form a block of variables
1 var (2 int 3 v2 String 4 )
From the above-mentioned var keyword can be seen, there is a var keyword more in line with the English habit, not only that, you can declare a number of variables, the variable with a keyword var package up, to form an entire block, so that the readability is better, will not let the variable disorderly put.
2.1.2 Variable Assignment
1 // Define a variable 2 int 3 // assigning values to a variable initialization 4 V1 = 10
The above two steps can be synthesized, that is, the definition plus initialization is done with a statement.
There are three different ways
1 int // How to use it correctly 1 2 var v2 = ten // Correct usage 2, the compiler can automatically deduce the type of v2 3 v3: = ten // Correct use of mode 3, the compiler can automatically deduce the type of V3
: = This operator contains the two-step operation of defining and initializing. The variable that appears in this operator should not have been defined, or it will compile an error.
Specifying a variable type is not required and looks a bit like a dynamic type language. But it's actually a static language.
The Go language provides multiple assignment functions, such as I,J value Exchange, which avoids the definition of many unrelated variables, which can affect code readability
I,j = J,i
2.1.3 Anonymous variables
When we are programming with traditional strongly typed languages, this is often the case when a function is called in order to get a value, but because the function returns multiple values, it has to define a bunch of useless variables. In go this situation can be combined to make
Use multiple returns and anonymous variables to avoid this ugly notation and make the code look more elegant.
2.2 Constants
Constants are known and immutable values during compilation, which can be numeric types (including integer, float, and complex types), Boolean types, String types
2.2.1 Literal constants
Literal constants refer to hard-coded constants in programs, such as
No type 2//Because 2 is within the domain of many numeric type definitions and does not have a type of 2 specified, it is an untyped, and of course can also specify 2L, which is a long type
Floating-point 3.1415926
Complex Type 3.2+12i
Boolean-True
String "true"
2.2.2 Constant definition
By using the Const keyword, you can specify the literal constant name
1 const Pi float64 = 3.1432423424234< Span style= "COLOR: #008080" >2 const zero = 0.0 3 const ( 4 size int64 = 10245 eof = -1// untyped integer constant 6 " 7 const u,v float32 = 0.3 // define multiple assignments 8 const a,b,c = 3,4, "true" // a = 3,b = 4,c = "true", untyped integral type and string constant
The right value of a constant definition can also be a constant expression of a compile-time operation
Const MASK = 1 <<3
The assignment of a constant is a compile-time behavior, so an rvalue cannot have any expression that requires a run time to produce a result (otherwise it is not a constant)
The following example causes a compilation error, and the constant is the value that has been determined before running.
Const HOME = os. GETENV ("HOME")
2.2.3 Pre-defined constants
The Go language pre-defines these constants as true, False and Iota.iota are special and can be thought of as a constant that can be modified by the compiler, which is reset to 0 when each const keyword appears, and then each time a iota appears before the next const appears, the number it represents will automatically increase by 1.
The following example can be used to understand the usage of iota basically:
1 Const(//Iota Reset to 02C0 = Iota//C0 = = 03C1 = Iota//C1 = = 14C2 = Iota//C2 = = 25 ) 6 7 Const ( 8A = 1 << iota//A = = 1 (iota is reset to 0 at the beginning of each const)9b = 1 << Iota//b = = 2Tenc = 1 << Iota//c = = 4 One ) A - Const ( -U = Iota * 42//U = = 0 theV float64 = Iota * 42//v = = 42.0 -w = Iota * 42//W = = - ) - Constx = Iota//x = = 0 (because iota is reset to 0) + Consty = Iota//y = = 0 (ibid.)
If the expression of the two Const assignment statement is the same, then the latter assignment expression can be omitted. Therefore, the first two const statements above can be abbreviated as:
1 Const(//Iota Reset to 02C0 = Iota//C0 = = 03C1//C1 = = 14C2//C2 = = 25 ) 6 7 Const ( 8A = 1 <<iota//A = = 1 (iota is reset to 0 at the beginning of each const)9B//b = = 2TenC//c = = 4 One)
Personally think that iota is not necessarily a good data type
2.2.4 Enumeration
Enumeration refers to a series of related constants, such as the following about the daily definition of one weeks. Using the example in the previous section, we see that you can define a set of constants in the form of a const followed by a pair of parentheses, a definition that is commonly used in the go language to define
The enumeration value. The go language does not support enum keywords that are explicitly supported in many other languages.
The following is a general enumeration notation, which defines a series of integer constants:
1 Const 2 Sunday = 3 4 5 6 7 8 9 numberofdays // This constant is not exported )
As with other symbols in the go language, constants beginning with uppercase letters are visible outside the package. In the example above, Numberofdays is private in the package and other symbols can be accessed by other packages.
2.3 Data types
Go supports the following data types