go language 20 hours from entry to the master (VI project management)

In the actual development work, directly call the compiler to compile and link the scene is less and less, because it would not have in the project as simple as between only one source code file, the source file and mutual dependencies. If such a file a file gradually compiled, and that nothing less than a disaster. Go language designers as an industry veteran, will naturally not ignore this. Go early language makefile as a temporary solution to the time of release introduces Go 1 Go so powerful command-line tool.

Go revolutionary at the command-line tool that completely eliminates the concept of the project file, complete with directory structure and package name to derive engineering structures and build order. Discussion of project management look more redundant, because it can be used directly get go run and go build the case for only one source file. Here we will use a project closer to virtual reality to show the basic engineering Go language management.

6.1 Workspace

6.1.1 Work Area

Go code must be placed in the workspace. Workspace is actually a directory corresponding to a particular project, it should contain three subdirectories: src directory, pkg directory and the bin directory.

l src directory: Go for the organization and save the file as a source code package. (Example: .go .c .h .s, etc.)

l pkg directory: used to store building code package after installation (included Go library source files) of ".a" archive via go install command.

l bin directory: pkg directory and similar, after the completion of the installation by go install command, save the executable file generated by the Go command source files.

Src directory used to contain all of the source code, a command-line tool Go mandatory rules, and pkg bin and you do not need to manually create, if necessary Go command-line tool will automatically create these directories during the build process.

Of particular note is that only when the environment variable GOPATH directory path contains only a workspace, go install command will install the command source bin directory to the current workspace. If the environment variable contains the directory path GOPATH multiple workspaces like this go install command execution will fail, then you must set the environment variable GOBIN.

6.1.2 GOPATH settings

In order to build this project, we need to first needed to be added to the root directory of the project environment variable GOPATH in. Otherwise, even in the same working directory (workspace), between the code can not complete the call by absolute code package path.

In the actual development environment, they tend to have more working directory. These working directory of the directory path needs to be added to GOPATH. When there are multiple directories, please note that the delimiter, when multiple directories is a semicolon for Windows, Linux system is the colon, when there are multiple GOPATH, the default will go get content on the first directory.

6.2 package

All Go language programs are organized into several groups of files, each file is referred to as a packet. Such codes can be used as each packet multiplexing section small, another is referenced items.

A source packet stored in one or more source files .go file name suffix, the suffix is ​​usually a directory path where the packet is introduced into the package path.

6.2.1 Custom packages

For a larger application, we should be separated into its functional logic units, respectively, to achieve a different package. Custom package we created the best placed GOPATH src directory (or a subdirectory src GOPATH).

In the Go language, source code included in the file name can be arbitrary. However, these arbitrary name of the source file must be based package declaration statement as the first line in the file, each packet corresponding to a single namespace:

package calc

复制代码

Package members to name the letter zoomed therefore especially ⼩ write access to the decision:

l public: therefore especially zoomed write letters, may be accessed outside of the package

l private: ⼩ therefore especially letter writing, only members can access the bag

** Note: ** the same directory can not define a different package.

6.2.2 main hull

In the Go language, named for the main package has a special meaning. Go language compiler will try to compile this package is the name of the binary executable file. All with Go language compiler executable must have a main package name. An executable program and only one main package.

When the compiler found the name of a package is main, it must also find a function called main (), and they will not create an executable file. main () function is the entry procedure, so without this function, there is no way to start the program execution. Directory name of the directory program compiled, would use the statement where the main package code as a binary executable file name.

6.2.3 main function and the function init

Go there function two reservations: init function (can be applied to all of the package) and the main function (applies only to package main). These two functions can not be defined at any parameters and return values. While a package which can write any number of init function, but whether it is for readability or maintainability later, we strongly recommend that all users in a package for each file just write an init function.

Go program will automatically call init () and main (), so you do not need to call these two functions anywhere. Each package in the init function are optional, but the main package must contain a main function.

Each package can contain any number of init functions that will be called when the program begins execution. All init function will be arranged to be discovered compiler to perform before the main function. init function is provided for use in the package, or other variables to be initialized preferentially done before running guidance.

Initialization and execution of the program, start from the main package. If the main package also import other packages, it will turn to import them at compile time.

Sometimes a package will be introduced into multiple packets simultaneously, then it will only be imported once (for example, many packages might fmt package will be used, but it will only be imported once, since there is no need to import several times).

When a packet is introduced if the packet also introduced additional packages, then the other packages will be introduced first in, then the packet level constants and variables are initialized these packets, and then performs the init function (if ),And so on. And all packages to be introduced are loaded, it will begin to packet-level constants and variables are initialized in the main pack, and then perform the main pack in the init function (if any), and finally perform the main function. The following figure illustrates in detail the whole process is performed:

 

Pictures .png

 

Sample Code Directory Structure:

Pictures .png

 

main.go following sample code:

// main.go
package main

import (
    "fmt"
    "test"
)

func main() {
    fmt.Println("main.go main() is called")     test.Test() } 复制代码

test.go following sample code:

//test.go
package test

import "fmt"

func init() {
    fmt.Println("test.go init() is called") } func Test() {     fmt.Println("test.go Test() is called") } 复制代码

operation result:

 

Pictures .png

 

6.2.4 Import packages

Import needs to use the keyword import, it will tell the compiler that you want to reference the code inside the package at that location. Package path can be a relative path, or an absolute path.

//方法1
import "calc"
import "fmt"

//方法2
import (
    "calc"
    "fmt"
)
复制代码

The standard library package will find the location of the installation of Go. Go package created by the developer looks at GOPATH environment variables specified directory. GOPATH specified in these directories is personal workspace developers.

If the compiler to search through GOPATH but could not find the package you want to import, then trying to build or run program execution will go wrong.

Note: If, after importing packages, functions, or which type of call will not report a compile error.

Pictures .png

 

6.2.4.1 point operation

import (
    //这个点操作的含义是这个包导入之后在你调用这个包的函数时,可以省略前缀的包名
    . "fmt"
)

func main() {
    Println("hello go")
}

复制代码

6.2.4.2 alias Operation

When START guide can be specified package member access shutter mode, as the packet ⽐ renaming to avoid conflicts with the same name:

import (
    io "fmt" //fmt改为为io
)

func main() {
    io.Println("hello go") //通过io别名调用
}

复制代码

6.2.4.3 _ Operation

Sometimes, a user may need to import a package, but the package does not require the reference identifiers. In this case, the blank may be used to rename the identifier _ introduced:

import (
    _ "fmt"
)

复制代码

_ In fact, the operation is the introduction of the package, rather than using a direct function inside the package, but the package calls inside the init function.

6.3 Test Case

6.3.1 test code

 

Pictures .png

 

calc.go code is as follows:

package calc

func Add(a, b int) int { //加
    return a + b
}

func Minus(a, b int) int { //减
    return a - b
}

func Multiply(a, b int) int { //乘
    return a * b
}

func Divide(a, b int) int { //除
    return a / b
}

复制代码

main.go code is as follows:

package main

import (
    "calc"
    "fmt"
)

func main() {
    a := calc.Add(1, 2)
    fmt.Println("a = ", a) } 复制代码

6.3.2 GOPATH settings

6.3.2.1 windows

Pictures .pngPictures .png

 

 

Pictures .png

 

6.3.2.2 linux

Pictures .png

 

6.3.3 compile and run the program

 

Pictures .png

 

Use 6.3.4 go install the

Set the environment variable GOBIN:

 

Pictures .pngIn the source directory, knocking go install:

 

 

Pictures .png

Guess you like

Origin www.cnblogs.com/ExMan/p/11540579.html