Chapter One first knowledge of Go language
Google is the main push of the go language, the open source community
Automatic garbage collection
function multiple return value
Memory Checker Tools Rational Purify, Compuware BoundsChecker, Parallel inspector, etc.
Built-in map and slice types (arrays)
Inheritance and overloading are not supported
Goroutine is a more lightweight and resource-saving process than threads
Reflection is supported, and instances cannot be constructed from a type name string
Using CGO to support the reuse of C
The executable must have a package with the name main and a main function (no parameters, no return value)
The log package provides the underlying logging functionality
Support GDB Debugging
Chapter II Sequential Programming
Variable declaration
var V1 [] String
VAR (
v1 int
V2 string
)
Declare and initialize the scene directly, Var can omit
var v3 = 10
v3:= 10
The compiler can infer the type automatically
Supports multiple assignments i,j = J,i
function has multiple return values but only one of them, you can use multiple assignments + anonymous variables
,, v3: = GetName ()
Constants are declared by the Const keyword
Const A,B,C = 3,4, "foo"
Built-in constants true false iota (self-increment, const reset to 0)
Capital letters beginning with constants, outside of package visible, otherwise private
base type BOOL int8 byte int16 int unit unitptr float32 float64 complex64 complex128 string Rune (character) error
compliant type pointer array SLI Ce map Chan struct interface
string can be read as an array subscript, but cannot be modified.
Built-in function Len () gets the string length or array length
Range keyword traversal array, container
for i,v: = Range array{
}
Array is a value type, when passed as a parameter, data is copied, The incoming array contents cannot be modified in the function
Slice is created based on an array or created by Make
Myslice: = Array[:5]
Myslice: = Make ([]int, 5)
Len () Gets the number of elements cap () Get capacity append () tile expansion append (Slice1,slice2 ...) three points represent the characteristics of the variable parameters of the Slice2, which are scattered after all elements. Copy () supports replication of slice
map using:
var mymap map[string] PersonInfo
Mymap = Make (map [string] PersonInfo), Mymap = Make ( Map [string] PersonInfo, +)
mymap["123"]=personinfo{"1", "Jack"}
Delete with delete (Mymap, "123")
Find value,ok:= mymap["123"] OK is true to find the
Conditional statement if: You do not need to enclose the condition in parentheses {} There must be a return not allowed
The conditional object of the switch statement can be omitted, followed by a conditional judgment on the case, no need to use break to exit the Fallthrough keyword
The loop supports for, does not support while and does while the ellipsis condition represents an infinite loop, such as for {sum + +}
Break can be followed by a label that supports breaking the loop of that layer.
Support Goto Statement
function definition func Add (a, B int) (ret int, err error) {}, the same parameter type can omit the type description, if there is only one return value, can be written as Func Add (b, int) int {}
For the names of functions, types, and variables, be aware of case, uppercase is open to other packages, lowercase is only open to this package.
Support for indeterminate parameter func myfunc (args ... int) An indeterminate parameter must be the last parameter; an indeterminate parameter is essentially an array slice, and an indeterminate parameter can be specified as any type, arg. (type) with interface to get the actual type of the parameter runtime.
Supports anonymous functions, supports closures, and can reference external variables in closures
Error handling is one of the highlights of the Go language
Error interface
Customize the error interface to implement the error method.
Defer keyword (deferred execution) to complete the release of resources, if there are more than one statement, you can use the anonymous function defer func () {} ()
The defer keyword follows the advanced post-out principle, and all a defer statement is executed first.
Use of panic and recover methods
Use the flag packet to parse the command line input parameter flag. String ("I", "infile", "1")
Master the use of standard libraries such as OS io Bufio StrConv.
Chapter III Object-oriented programming
Support for adding methods to any type
Type Integer int
Func (a integer) less (b integer) {
Return a<b
}
Most types of the go language are value types, including basic types and conforming types
Slice map channel and interface look like reference types.
Definition of structural body
Type Rect struct {
x, y int
}
Initialize rect1 = new (Rect); Rect2=&rect{};rect3=&rect{1,2};rect4=&rect{x:1,y:2}
Structs implement the ability of Java inheritance and method coverage by anonymous combination, and if the method is not rewritten, it can be integrated directly.
A normal struct cannot be assigned nil and cannot be judged by = =, the struct pointer can be assigned nil, even if the ==nil,println can output a struct type.
Non-intrusive interface
Type IFile Interface {
Read (buf []byte) (n int, err error)
}
Interface Assignment Value
var a Integer = 1
var b lessadder = &a
Determine if an instance implements an interface
var file1 Writer = ...
If file6,ok:= file1. (*file); OK {
}
Switch V: = v1. (type) {
Case INT:
...
}
It can also be reflect by means of reflection. TypeOf ()
interfaces, like structs, support anonymous combinations
Null interface interface{} can be viewed as any type that can point to any object
Pointers to interface objects and interface objects can be assigned nil or judged to be equal.
Say that a pointer object is assigned a value of nil, and then assign the pointer object to an interface, when the interface object is not nil, why??
Fourth Chapter concurrent Programming
Common concurrent communication models: shared data, messages
Threads and processes are up to 1W in number, and can easily be created by the co-process.
Goroutine supports the process of transferring CPU to other Goroutine in the system call operation.
Channel: The ability to implement a lock by passing messages between multiple goroutine.
Statement var channame Chan ElementType; var m map[string] Chan bool
Assignment Ch:=make (chan int)
Writing to Cha <-Chanvalue writes will cause the program to block until there are other goroutine reading data from the channel
Read chanvalue: =<-ch, if the data was not previously written, the read will also cause the program to block.
SELECT statement
for {
Select {
Case CH<-0:
Case CH<-1:
}
i:= <-ch
}
Buffered channel, which does not block until the channel buffer is full
var c = make (chan int, 1024)
One-way channel that can restrict read-only or write-only
Close Channel Cloes (CHAN)
Determine if a channel is closed x,ok:=<-ch OK if return false, it means that Chan has been closed.
Multi-core parallelization: Runtime is required. Gomaxprocs (16) to explicitly tell the go compiler the CPU's number of cores. Numcpu () method gets the number of CPU cores
Sell time Slice: gosched ()
Sync Lock: Sync. Mutexes and Sync.rwmutex (single-write multiple-read)
Global uniqueness Operation Once.do () in sync. Once bag
The fifth Chapter network programming
Establish links
Conn,err:=net. Dial ("TCP", "192.168.0.10:2100") ==net. Dialtcp
Conn,err:=net. Dial ("UDP", "192.168.0.10:2100") ==net. Dialudp
Conn,err:=net. Dial ("ip4:icmp", "www.baidu.com")
Conn,err:=net. Dial ("ip4:icmp", "www.baidu.com")
Conn.write ()
Conn.read ()
Net. RESOLVETCPADDR () Resolve address and port number
Net. PARSEIP () Verifying IP address validity
Net. Ipv4mask Creating a subnet mask
Net. Defaultmask ()
Net. RESOLVEIPADDR ()/net. Lookuphost () Find IP address based on domain name
HTTP Encapsulation
http. Get ("www.baidu.com")
http. Post ()
Server-Side HTTP. Listenandserve ()
Supports RPC programming
The default data structure codec for Go is with GOB, which supports custom
Go Language Programming summary