Blockchain is a complex and complex discipline that combines cryptography, consensus algorithms, intelligent contracts, and super-ledgers. Yun Jong said that the quality of the teachers in the blockchain training institutions and their perceived level in the blockchain industry were uneven, and the average consumer was not.
The go language is primarily used as a server-side development, targeting the development of "large software", suitable for many programmers to develop large-scale software together, and the development cycle of long-term support for cloud computing network services. The go language enables programmers to develop quickly, and it makes it easier for programmers to maintain and modify the software as it grows. It combines the efficiency of a traditional compiled language with the ease of use and expression of a scripting language.
If the entry of a function is interface{}, there are several ways to get the arguments:
Import "FMT"
Func Main () {
V: = "Hello World"
Fmt. Println (typeof (V))
}
Func typeof (v interface{}) string {
Return to FMT. Sprintf ("%T", V)
}
2 Reflections:
Import (
"Reflect"
"FMT"
)
Func Main () {
V: = "Hello World"
Fmt. Println (typeof (V))
}
Func typeof (v interface{}) string {
return reflect. TypeOf (v). String ()
}
3 Type Assertion:
Func Main () {
V: = "Hello World"
Fmt. Println (typeof (V))
}
Func typeof (v interface{}) string {
Switch T: = V. (type) {
Case INT:
return "int"
Case Float64:
Return "Float64"
... etc
Default
_ = t
Return "Unknown"
}
}
In fact, the first two are all used reflex, FMT. The final call in Printf ("%T") is still reflect. TypeOf ().
Func (P *pp) Printarg (Arg interface{}, verb rune) {
...
Special processing considerations.
%T (the value ' s type) and%p (its address) is special; We always do them first.
Switch verb {
Case ' T ':
p.fmt.fmt_s (reflect. TypeOf (ARG). String ())
Return
Case ' P ':
P.fmtpointer (reflect. ValueOf (ARG), ' P ')
Return
}
Reflect. The parameter of TypeOf () is how does the reflection of V Interface{},golang do?
In Golang, interface is also a struct, recording 2 pointers:
Pointer 1, pointing to the type of the variable
Pointer 2, which points to the value of the variable
As below, the structure of the empty interface is the above 2 pointers, the first pointer is of type rtype struct, and the non-null interface has more information to carry (such as which methods the interface implements), so the type of the first pointer is Itab, and the dynamic type of the variable is recorded in Itab: Typ *rtype.
Emptyinterface is the header for a interface{} value.
Type emptyinterface struct {
Typ *rtype
Word unsafe. Pointer
}
Nonemptyinterface is the header for a interface value with methods.
Type nonemptyinterface struct {
See.. /runtime/iface.go:/itab
Itab *struct {
Ityp *rtype//static interface type
Typ *rtype//Dynamic Concrete type
Link unsafe. Pointer
Bad Int32
Unused int32
Fun [100000]unsafe. Pointer//Method table
}
Word unsafe. Pointer
}
Let's take a look at reflect. TypeOf ():
TypeOf returns the reflection type that represents the dynamic type of I.
If i is a nil interface value, TYPEOF returns nil.
Func TypeOf (i interface{}) Type {
Eface: = * (*emptyinterface) (unsafe. Pointer (&i))
Return ToType (Eface.typ)
}
typeof sees an empty interface interface{}, which translates the address of the variable to an empty interface, and then returns the resulting Rtype to the type interface. It is important to note that when calling reflect. Prior to TypeOf, an implicit type conversion has occurred, with the conversion of a specific type of NULL interface. This process is relatively simple, as long as the copy Typ *rtype and Word unsafe. Pointer can do it.
For example w: = OS. Stdout, the interface value of the variable is in memory.
So for the third, how does the type assertion determine if it is an interface? Back in the first place, in Golang, the interface is a loosely coupled concept, whether a type implements an interface or not, which is to see if the type implements all the functions required by the interface, so the method of type assertion is to check that the type implements all the functions required by the interface.
Day k8s code, you can see the use of more types of assertions:
Func leastrequestedprioritymap (pod *api. Pod, Meta interface{}, NodeInfo *schedulercache. NodeInfo) (Schedulerapi. Hostpriority, error) {
var nonzerorequest *schedulercache. Resource
If Prioritymeta, OK: = Meta. (*prioritymetadata); OK {
Nonzerorequest = Prioritymeta.nonzerorequest
} else {
We couldn ' t parse metadata-fallback to computing it.
Nonzerorequest = getnonzerorequests (pod)
}
Return calculateunusedpriority (pod, nonzerorequest, NodeInfo)
}
The implementation of the type assertion is in Src/runtime/iface.go (?), but this code is not understood, and then updated later.
Func Asserti2i2 (Inter *interfacetype, I iface) (R iface, b bool) {
tab: = I.tab
if Tab = = Nil {
Return
}
If Tab.inter! = Inter {
tab = Getitab (Inter, Tab._type, True)
if Tab = = Nil {
Return
}
}
R.tab = tab
R.data = I.data
B = True
Return
}
Func Asserte2i2 (Inter *interfacetype, E eface) (R iface, b bool) {
T: = E._type
If T = = Nil {
Return
}
tab: = Getitab (Inter, T, true)
if Tab = = Nil {
Return
}
R.tab = tab
R.data = E.data
B = True
Return
}
high-energy early warning, brother even education blockchain live course August continues to hit the hot!
The original price of 1188 Yuan of 12 block chain advanced courses, now only need 1 Yuan!
Also can receive free "Go Language foundation actual combat project development" and "Go language Advanced Combat Project development" textbook Two!!
Limited time limit!! First come first served!!
Http://www.ydma.cn/open/course/24
Pay attention to Brother chain public number can get more Blockchain technology dry!!!