Go Language Quest-11 (original)

Source: Internet
Author: User

The previous article mainly studied the slices of the go language and map. This article focuses on interface, reflection, and error and exception handling in the go language.

Interface:

Recall that, in Java, there is the notion of a parent class and a subclass, and the parent class is primarily to provide an abstract public way for subclasses to extend their functionality based on this. Java also has abstract classes and interfaces

A data type, interface, is also available in the Go language. The interface of the go language is to define all the common methods, and the other type implements the interface as long as it implements the method within the interface. (Is it a Java-like interface?) )

The go language defines the syntax format for the interface:

Type interface Name Interface {

interface method ();

}

Go Language Interface Combat:

In order to see the effect, we first define an interface to eat, because of the differences between Chinese and Western food culture, we use chopsticks, crooked nuts with a knife and fork, for this difference we can define two structures; we let both structures implement the eating interface, because the interface cannot be directly new But you can have the new subclass (that is, the struct that implements the interface), so you can have the following code:

Interface-1

Well, we can also continue to expand:

Interface-2

The results are as follows:

Test results

Expansion: Empty interface

Empty interface (interface{}) does not contain any method, and because of this, all types implement an empty interface. The null interface does not have any effect on the description (because it does not contain any method), but the null interface is useful when we need to store any type of numeric value, because it can store any type of value. It is somewhat similar to the void* type of C language. Such as

Null interface

A function takes interface{} as a parameter, then he can accept any type of value as a parameter, and if a function returns interface{}, it can return any type of value. Any type of value this benefit is easy to expand

In summary: interface is a set of abstract methods that must be implemented by other non-interface types and not self-fulfilling. Go through interface realized duck-typing: "When you see a bird walking like a duck, swimming like a duck, barking like a duck, then this bird can be called a duck."

Reflection:

The Go language implements reflection (which in the end appears to have a similar effect to Java's reflection), so-called reflection is a mechanism that checks the state of a program at run time, gets the attribute value, and can be assigned a value. The go language uses the reflection function to use the reflect package.

The go language uses reflection generally into the following three steps:

A: First, you need to convert objects that need to be reflected into reflect objects (reflect. Type or reflect.value, calling different functions depending on the situation). The two ways to get these are:

T: = reflect. TypeOf (i)//get the type of metadata, through t we can get all the elements inside the type definition

V: = reflect. ValueOf (i)//get the actual value, through v we get the value stored in it, we can change the value

B: After turning to the reflect object, we can convert the reflect object to the corresponding value, for example

Tag: = T.elem (). Field (0). Tag//Get the Label field defined in the struct

Name: = V.elem (). Field (0). String ()//Gets the value stored in the first field

C: The reflected field must be modifiable (that is, the API reflected in Java, allowing reflection)

Here is an example of a simple reflection, by getting the value of x, and then the reflection call, in modifying the value of the operation:

Reflection

Error and Exception handling:

Error refers to a problem where there may be a problem, such as the failure to open a file, which is expected

Exceptions refer to problems where problems should not occur, such as referencing null pointers, array out-of-bounds, divisor 0, and so on.

So:

Go introduces the error interface as the standard mode of fault handling, and if the function returns an error, the return value type contains error;

Go introduces two built-in functions panic and recover to trigger and terminate the exception-handling process, while introducing the keyword defer to defer execution of the function behind defer.

Error and exception-based processing a very good blog is recommended here:

Golang correct posture for error and exception handling

The content of the interface, reflection, and error and exception handling in the go language is essentially over.

If this article is helpful to you, I hope you crossing leave a valuable star, thank you.

Ps: Copyright belongs to the author, reproduced please indicate the author, commercial reprint please contact the author to obtain authorization, non-commercial reprint please indicate the source (beginning or end please add reprint source, add original URL address), article do not abuse, also hope that we respect the author's labor results

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.