The interface of the Go Language Foundation (vi)

Source: Internet
Author: User
Tags emit

1, the definition of the interface reserved word interface is given a variety of different meanings. Each type has an interface, which means that the method collection defines the structure and structure of a method for that type: type S struct {i int}func (P *s) Get () int {return p.i}func (P *s) Put (v int) {p. i = v} can also define an interface type, just a collection of methods. This defines an interface with two methods I:type I interface {Get () intput (int)} is a legitimate implementation for an interface i,s because it defines the two methods required for I. Note that even if you do not explicitly define S to implement I, this is also true.  func f (P I) {//defines a function that accepts an interface type as a parameter fmt. Println (P.get ())//P implements interface I and must have get () method p. The put (1)//Put () method is similar to}  where the variable p holds the value of the interface type. Because s implements I, you can call the F pointer to it passing the value of type S: Var s; F (&s) Gets the address of s, not the value of S, because the method is defined on the pointer of S, which is not required--you can define the method to accept the value--but then the put method does not work as expected.  go Union interface value, static type check, runtime dynamic conversion, and no need to explicitly define type adaptation an interface   define another type implements interface I:type R struct {I int}func (P *r) Get () int {ret Urn p.i}func (P *r) Put (v int) {p.i = v}  function f can now accept variables of type R S. Suppose you need to know the actual type in function f. You can use type switch to get it in Go. Func f (P I) {switcht:=p. (type) {//type judgment. Use (type) in a switch statement. Save type to Variable t;case *s: The actual type of//p is the pointer of S, case *r://P is the actual type of r pointer; case S://P is the actual type of s;case R://P The actual type is R;default://Implements the other type of I. }}  in SWIIt is illegal to use (type) outside of tch. Type judgment is not the only way to get the type at run time. To get the type at run time, you can also use "comma, OK" to determine whether an interface type implements a particular interface: if T, OK: = something. (I); OK {//For some implementations of the interface I/T is its own type}  determines that a variable implements an interface that can be used: t: = something. (I)   2, NULL interface because each type can match to an empty interface: interface{}. We can create a normal function that accepts an empty interface as a parameter: Func g (something interface{}) int {return something. I). Get ()}  The return something in this function. (I). Get () is a bit of a trick. The value something has type interface{}, which means that the method has no constraints: it can contain any type. (I) is a type assertion that is used to convert an something to an I-type interface. If you have this type, you can call the Get () function. Therefore, if you create a new variable of type *s, you can also call G (), because *s also implements an empty interface. s = new (s) fmt. Println (g (s)); The operation of call G will not be problematic and will print 0. If the call to the G () parameter is not implemented I will bring a trouble: i:=5← declaration i is a ' damned ' intfmt. Println (g (i)) This can be compiled, but when run: Panic:interface Conversion:int is not main. I:missing method Get This is absolutely no problem, built-in type int has no Get () methods.    3, method method is the function that has the receiver. Methods can be defined on any type (except for non-local types, including built-in types: int types cannot have methods). However, you can create a new integer type that has a method. Type Foo intfunc (self Foo) Emit () {fmt. The method interface for Printf ("%v", self)}type Emitter interface {Emit ()} interface type is defined as a collection of methods. Method contains the actual code. In other words, an interface is defined, and the method is implemented. So, the receiver cannot be defined as an interface type, and doing so will cause invalid receiver type ... Compiler error. Note: The recipient type must be T or *t, where T is the type name. T is called the recipient base type or short base type. The underlying type must not make a pointer or interface type, and is defined in the same package as the method.   interface pointers creating pointers to interfaces in Go are meaningless. It is also illegal to actually create a pointer to an interface value.   4, interface name   According to rules, single-method interface named method name plus-er suffix: reader,writer,formatter, etc. There are a bunch of such names that efficiently reflect their responsibilities and the names of the functions they contain. Read,write, close,flush,string, etc. have a normative statement and meaning. To avoid confusion, do not let the same name be used unless there is a similar declaration and meaning. Conversely, if a type implements the same method as a well-known type, the same name and declaration are used, and the string conversion method is named string instead of ToString.  5, introspection, and reflection learn about the "tags" (named "NAMESTR") defined in the definition of person. In order to do this, a reflect package is required (there is no other way in Go). Remember that viewing a label means the definition of the return type. Therefore, use the reflect package to indicate the type of the variable and then access the label.

The interface of the Go Language Foundation (vi)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.