This is a creation in Article, where the information may have evolved or changed.
Recently, because departments want to unify the service-side development of all projects, that is, all new projects will have the same set of frameworks in the future, thus helping developers to achieve low-cost conversions when participating in multi-project development. As a result, technical directors have designated a colleague as the driving force in this area to promote the selection of language and framework for the new project. Judging from the current opinion, language selection may be translated into the go language. So I also started the GO study. In the learning process also found a lot of fun things, the right to record:
I. Definition and reference of a package
The go language can also define packages in languages like Java, but go has strict requirements for package definition:
1. You can only define code for the same package under the same folder. As in the following tree, code definitions for files such as Bubble.go,insert.go are used by the package Test_sort
src/
└──test_sort
├──bubble.go
├──insert.go
├──quick.go
└──selection.go
2. Because the path retrieval of the package in the go language is specified by the GOPATH environment variable, it must be specified in the GOPATH environment variable to be used, even if it is a package developed by itself. If you use the command: Export gopath= $GOPATH:/home/haust/go or add the environment variable in the configuration file, Linux under the user root directory to add the above statement, Under Windows, add the Gopath variable to the system environment variable and define the content or modify the existing environment variable directly
3. The directory of the referenced package must contain the SRC directory, and all packages are included in the SRC directory to be referenced correctly, otherwise the definition of the package cannot be found when compiling the code
4. The reference package in the code is specified by the import identifier, and if it is a single package reference, you can use the import "FMT" method directly, but if multiple packages must be used:
Import ( "FMT" Import (
"Math/rand") or "FMT"
"Math/rand"
)
That is, each package must have a newline reference, or the program will error
Ii. Declaration and assignment of variables
Put a few lines of code first:
var a int a = 3 //a is declared after the assignment var b int = 5 //b at the same time the declaration assignment c: = 4 //c Declaration and assignment
In the three declarations above, it is important to note the difference between the assignment symbols, the variables that are declared with the VAR flag, and the variables that are used: = the way life is different, followed by the introduction of the use of the array will show you
Iii. Definition of functions
1. In the function definition in the Go package, externally visible function name must be uppercase letters, other function names do not require, see the definition of type
2. function definition must start with a func identifier
3. The return value of the function is defined after the parameter list
4. Functions can have multiple return values, but multiple return values must be enclosed in ()
5. A function with the same name cannot appear within the same package
such as: Func retself (a int)int//single return value declaration
Func retself (a int, b int) (int, int)//Multiple return value declaration
Iv. type definition
Although there are no classes in go, there are also type definitions, which are defined in the same way as C + + classes, but with few differences, as follows:
Type Rect struct {
width, Height int
}
1. Initialization of the type is very simple, and using {} to wrap the initialization list after the type name. Two-way initialization is supported in Go
(1) named initialization, such as rect: = Rect{width:3, Height:4}. This initialization is relatively simple and easy to understand.
(2) Anonymous initialization, such as rect: = Rect{3, 4}. This initialization method is initialized according to the initialization list and the members of the type, that is, the result is width = 3, height = 4
2. Definition of type member functions
In the Go type definition, the same support member function, the following example:
Func (Rect rect) area () int {
Return rect.width * rect.height
}
Its invocation is simple: the Rect.area () call can
Five, interface definition
The definition reference is as follows:
Type Shape Interface { Perim () int}type Rect struct { width, height int}type trian struct { A, B, C Int}func ( R Rect) Perim () int { return 2*width + 2*height}func (t Trian) Perim () int { return a + B + c}
In use is also exceptionally simple, as follows:
Func Calcperim (s Shape) int { return S.perim ()}func main () { rect: = rect{3, 4} Trian: = Trian{1, 2, 3} Ca Lcperim (Rect) Calcperim (Trian)}