Method
Although there is no class, there is still method-go
-Implement a combination with a type by displaying a description of receiver
-method can only be defined for a type in the same package
-receiver can be a value or pointer of type
-No method overloads exist
-You can use a value or a pointer to invoke a method, and the compiler will automatically complete the conversion
-In a sense, the method is the syntactic sugar of the function, because receiver is actually the first parameter received by the method
-If the external structure and the embedded structure have a method with the same name, the method of calling the external structure is preferred
-type aliases do not have the methods that are included with the underlying type
-Method can call a non-public field in a struct
Interface interface
-an interface is a collection of one or more method signatures
Reflective Reflection
Concurrent concurrency
Channel
-channel is the bridge of goroutine communication, mostly blocking synchronous
-Created by make, close off
-channel is a reference type
Package Main
Import "FMT"
Import "Time"
Func Main () {
C: =make (chan bool)
Go func () {
Fmt. Println ("Go Go Go")
C <-true
}()
<-c
Time. Sleep (2*time. Second)
}
-You can use the for range to iterate through the channel
Package Main
Import "FMT"
Import "Time"
Func Main () {
C: =make (chan bool)
Go func () {
Fmt. Println ("Go Go Go")
C <-true
Close (c)
}()
For V: =range c{
Fmt. Println (v)
}
}
-Single or bidirectional channels can be set
-You can set the cache size without blocking until it is filled
Package Main
Import "FMT"
Import "Runtime"
Func Main () {
Runtime. Gomaxprocs (runtime. NUMCPU ())
C: =make (Chan bool,10)
For I: =0;i<10;i++{
Go Go (c,i)
}
For I: =0;i<10;i++{
<-c
}
}
Func Go (c chan bool,index int) {
A: =1
For I: =0;i<100000000;i++{
A+=1
}
Fmt. Println (Index,a)
C<-true
}
Equivalent//
Package Main
Import "FMT"
Import "Runtime"
Import "Sync"
Func Main () {
Runtime. Gomaxprocs (runtime. NUMCPU ())
WG: =sync. waitgroup{}
Wg. ADD (10)
For I: =0;i<10;i++{
Go Go (&wg,i)
}
Wg. Wait ()
}
Func Go (WG *sync. Waitgroup,index int) {
A: =1
For I: =0;i<100000000;i++{
A+=1
}
Fmt. Println (Index,a)
Wg. Done ()
}
Select
-can handle the sending and receiving of one or more channel
-Processing in random order when there are multiple channel available
-Free Select to block the main function
-Can set timeout
This article is from the "Dbaspace" blog, make sure to keep this source http://dbaspace.blog.51cto.com/6873717/1963461
Go language method, interface, reflection, select