This is the second day of my participation in the More text Challenge. For more details, see more text Challenge
Tip: This series is for readers with a persistent urge to Go
Notice before reading: In this chapter on Program Structures, we will focus more on the structures specific to Golang and not on the structures (naming, declaring, assigning, scoping, etc.) similar to those found in other languages such as C and Python.
Golang type alias
The type alias feature was introduced in GO1.9 where the two built-in types byte and Rune are aliases for uint8 and Int32. A type alias is literally a different name, they’re the same type.
Type declaration statements typically occur at the package level, so if the first character of a newly created type name is capitalized, it can be used outside the package.
Fix the problem: Enable progressive code fixes during large-scale refactoring, specifically moving types from one package to another so that code that references the old name interoperates with code that references the new name. Type aliases can also be useful for allowing large packages to be broken up into multiple implementation packages using a single top-level export API, and for experimenting with extended versions of existing packages.
Definition method:
type T1 = T2
Copy the code
Proposal address: 18130-type-alias
2. Variable initialization
- If the initialization expression is omitted, the variable is initialized with a zero value, as follows:
int
–>0
bool
–>false
string
–>””
Interface types: Slice, pointer, map, chan, function –>nil
Array or structure: Each element or field is initialized with a zero value of its corresponding type
The zero-value initialization mechanism ensures that each declared variable always has a well-defined value, a feature that can save a lot of extra work
- The return value of the function initializes the variable, such as the acquisition of err. The variables on the left side of the assignment statement and the resulting values on the right must have the same data type.
if err:=http.Get(addr),err{
//
}
Copy the code
- The initialization expression can be a literal or arbitrary expression. Variables declared at the package level are initialized before the main entry function executes, and local variables are initialized when the declaration is executed
- Brief type (
: =
Local variable declarations can only be used for local variable declarations. And the short variable declaration statements assign values to those variables that have been declared. For example, the second declaration declares only one variable, out.
in, err := os.Open(infile)
// ...
out, err := os.Create(outfile)
Copy the code
- The animate variables within the function must be used, but underscores can be used
_
Discarded.
3. Life cycle of variables
For variables declared at the package level, their lifetime is consistent with the lifetime of the entire program.
In contrast, the life cycle of a local variable is dynamic: it starts with a declaration that creates a new variable until the variable is no longer referenced, and then its storage space may be reclaimed.
The parameter variables and return value variables of a function are local variables. They are created each time the function is called.
Golang Package and Golang Module
1. package
: Encapsulation of a set of functions or functions, i.e. to avoid duplication of wheels.
After 1.12 (module was introduced), a go folder is created in the user directory as the default GOPATH, and go Get downloads remote packages to the GOPTH directory PKG /mod. GOROOT is the GO file and installation directory. $GOROOT/ SRC is a built-in standard library.
The GOPATH directory has downloaded the package
The $GOROOT/ SRC directory has built-in packages
To import a shared package without using Golang Module, you need to use go Get to download the package locally, and then you can reference the package with import
2. module
: Manage packages, that is, manage application dependencies and package versions.
With the introduction of Module, the project directory is no longer limited to GOPATH/ SRC. You can create your project in any folder and initialize it using go mod init. The following initializes a module to github.com/csgo and generates the go.mod file in the current folder.
[root@VM-0-5-centos water_man]# ls go.mod water_man.go [root@VM-0-5-centos water_man]# cat go.mod module github.com/csgo Replace github.com/csgo/rush_b =>.. /rush_b // Redirect package to.. / rush_B require github.com/csgo/rush_b v0.0.0-00010101000000-000000000000 // Non-standard library dependencies and versionsCopy the code
Custom package in upper level directory:
The Go Module imports three-party packages based on the dependency references of the go.mod. If the local cache is not found, it will be pulled from remote cache. That is, go get is used if github.com/csgo/rush_b is not redirected locally. When the Go Module downloads the remote package, it automatically updates the go.mod.
Note that custom package functions and variables should be capitalized if they need to be referenced externally, that is, package-level names start with a capital letter.
Feel free to point out any shortcomings in this article in the comments section.
Welcome to collect, like and ask questions. Keep an eye on top water cooler managers and sometimes do more than just boil hot water.