Catalog
1. regexp package: regular expression
2. sync package: lock and sync
2.2 sync.RWMutex read / write lock
1, Standard library
Built in packages with common functions such as FMT and OS are listed as follows:
unsafe: contains some commands that break the "type safety" of the Go language, which will not be used in general programs and can be used in the call of C/C + + programs.
syscall-os-os/exec:
os: It provides us with a platform independent operating system function interface, which adopts class Unix Design, hide the differences between different operating systems, so that different file systems and operating system objects are consistent.
os/exec: Provides a way for us to run external operating system commands and programs.
syscall: The underlying external package provides the basic interface for the underlying calls of the operating system.
archive/tar and / zip compress: the ability to compress (decompress) files.
fmt-io-bufio-path/filepath-flag:
fmt: The function of formatting input and output is provided.
io: It provides basic input and output functions, most of which are packaged around system functions.
bufio: Encapsulation of buffered I / O function.
path/filepath: The path of the target filename used to operate on the current system.
flag: Action on command line arguments.
strings-strconv-unicode-regexp-bytes:
strings: Provides an operation on a string.
strconv: Provides the ability to convert strings to base types.
unicode: by unicode Type provides special functionality.
regexp: Regular expression function.
bytes: Provides the operation of character type segmentation.
index/suffixarray: Substring quick query.
math-math/cmath-math/big-math/rand-sort:
math: Basic mathematical functions.
math/cmath: Operation on complex number.
math/rand: Pseudo-random number generation.
sort: Sorting and customizing collections for arrays.
math/big: The realization and calculation of large numbers.
Container - / list ring heap: implements the operation on the collection.
List: double linked list.
ring: ring list.
time-log:
time: Basic operation of date and time.
log: Record the logs generated when the program is running.
encoding/json-encoding/xml-text/template:
encoding/json: Read and decode and write and encode JSON Data.
encoding/xml:Ordinary XML1.0 Parser.
text/template:Generating image HTML The same data-driven template with mixed data and text.
net-net/http-html:
net: Basic operation of network data.
http: Provides an extensible HTTP Server and basic client, parsing HTTP Requests and responses.
html: HTML5 Parser.
Runtime: interaction at run time of the Go program, such as garbage collection and orchestration creation.
reflect: realize the reflection of any type of variable through the program runtime.
1. regexp package: regular expression
For regular expression matching:
Simple mode: using match method
Complex pattern: first, the regular pattern is transformed into a Regexp object through the Compile method. Then we use the match, find, replace and other functions of the object.
func main() { matched, err := regexp.Match(`foo.*`, []byte(`seafood`)) fmt.Println(matched, err) matched, err = regexp.Match(`bar.*`, []byte(`seafood`)) fmt.Println(matched, err) matched, err = regexp.Match(`a(b`, []byte(`seafood`)) fmt.Println(matched, err) }
The operation results are as follows:
2. sync package: lock and sync
For inter thread synchronization
2.1 sync.Mutex mutex
Its function is to guard the critical area entrance to ensure that only one thread can enter the critical area at the same time.
2.2 sync.RWMutex read / write lock
It can allow multiple threads to read variables at the same time through RLock(), but only one thread can write. If you use Lock(), it will work the same as a normal Mutex.
2.3 once.Do(call)
There is also a convenient method for variables of type Once in the package, which ensures that the called function can only be called Once.
func (o *Once) Do(f func())
There is no lock mechanism for map type to achieve this effect (for performance reasons), so map type is non thread safe. When a shared map type data is accessed in parallel, the map data will fail.
2, Use of packages
We know that package is the main way to organize and compile code in Go language.
1. Custom package
When writing your own package, use small, lowercase words that don't contain an underscore to name the file.
By convention, subdirectories and packages are closely related: in order to distinguish, different packages are stored in different directories, and each package (all go files belonging to this package) is stored in the same subdirectory as the package name
2. reference package
When we want to use the resources in the package, we need to introduce the package
import "path or URL address of package" path refers to the relative path of the current directory
import "./pack1/pack1" import "github.com/org1/pack1" //The package imported in this way can directly use the resources in pack1 without the prefix of pack1.Func() import . "./pack1" //This import method can help us check the package pack1, only execute its init function and initialize the global variables in it import _ "./pack1/pack1"
3. Initialization of package
Program execution: 1. Import package 2. Initialize main package 3. Call main function
A package may have multiple init functions even in a source file. The init function cannot be called.
The imported package is initialized before the package itself is initialized, while a package can only be initialized once during the program execution.