在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
Go InterfaceIn many language to communicate to the computer , we have the interface structure to construct the rule between the people and the computer data structure . What is the interface ? In some time I have do this thinking that the interface is the rule for the all class which implement this interface and then will act as a same role For a sample example in real world . we could know that the student is a role in school not only the perimary school middle schoold and the high school even in university . The student must do their duty to do in school , so we can call them be student role . right ? And the the all structure programming language had , we should know that the design method we could let the same behaviors to an interface and let the class to implement the interface and we also need some variables to define the base attribute of this class so the easy method let we know how to implement a real world into computer system . In GoIn Go we get this reality like java the interface{} is the Any type like Java Object type. But what is the type or what is the type information ? I think that the interface is a type absolution in action . The type in programming language just do this work to allocate the memory space and originaze the all pointer . But anything exculd the flow the system don’t do like this . Define an interface in go is really easy
We can use this grammer to create an interface and then we just let the struct called class to implement the all methods . We don’t need to do the implementation explicitly like in java use the implement key word , the compiler consider that a class override all method in an interface then it can belive the class is an instance for this interface . Like in Go standard library the File implement the io.Reader and io.Writer then we can use the file to write and read , and the io.Reader and io.Writer just have method like the interface Read and Write , but don’t have any implemention and File will implement the all method to read and write , but how to write the File type don’t need to know . And interface in Go can inherite from other interfaces like
We can know that the ReadWriter must have two methods and then we have aggregating the two interface we defined before I call this as MixIn character in go . and the all examples display the method to define a interface and inherite to contruct a new interface . polymorphismThe interface instance will be initlize to be nil and nil , and the OOP model in Go most like the OOP in Objective-C , called communication model , the class called as reciver and the methods called selector and the call process called message delivery .
When define the buf variable then the buf will be nil and nil also be the interface as Reader and then when call , the buf will be nil so the message can be failure . How polymorphism do in Go I think like java we just assgin a class which implement the interface we mention before , then we can create many class like the class , and then we just use the interface type pointer to point the class then we can use the method in polymorphism Interface assignmentThe memory model like many language , the class construct with pointer and constraint pool like java , we can define the const not only const but also the literal value and these variables can be saved in the meemory area in the .css segment and the method can be saved in the memory segment , and different type struct implement the only interface define in the all program will be in many memory segment . And when the method call then the runtime can find the real address of the method which call in the process . T and T* is the different type in Golang like this
The wrong sign do what let this statement to be wrong , T and T is two different type , when we use the s to assign to i then we assign the type T to a interface but the T don’t implement the method in the interface but the T implement the methods ,but why ,because the Go hava carried the truth of the OOP to us which save the self or this pointer to the struct then we can get or set the number but we also can let self as the T type but as we know the Go call a function/method just copy the arguments to the list of argument we modify the arguments struct don’t do any effect . Some examplestandard-library flagthe flag package which use to handle the commind line arguments
after we create the type and method and function we should use go install command to install the libray in computer .
Go Package managementwe can see the package inner go file as a moudle to manage , and any module can implement the init() function not only just once and the runtime find moudule prcesure like this |
请发表评论