Go Language Interface Definition and usage example _golang

Source: Internet
Author: User

This example describes the go Language interface definition and usage. Share to everyone for your reference, specific as follows:

In go, the interface interface actually has no difference with the interface meaning of other languages. Interface understand it as a type of specification or convention. Does one type "implement" an interface? It depends on whether this type implements all the methods defined in the interface.

1. Definition and use of interfaces.

Like what

Copy Code code as follows:
Type I interface{
Get () int
put (int)
}

This passage defines an interface that contains two function get and put

Well, one of my interfaces implements this interface:

Copy Code code as follows:
Type S struct {val int}
Func (this *s) get int {
Return This.val
}
Func (this *s) put (v int) {
This.val = V
}

This structure S is the implementation of the interface I

2. Null interface

For the null Interface interface{} is really similar to the concept of generics. Any type implements an empty interface.

Here's an example:

A function implements such a function:

With any object as an argument, if the object is implementing interface I, then call the Get method of interface I

Many languages are the same logic:

Copy Code code as follows:
function g (obj) {
if (obj is I) {
Return (I) obj. Get ()
}
}

This is done in the go:
Copy Code code as follows:
Func g (any interface{}) int {
Return to any. (I). Get ()
}

Here's any. (I) is it very semantic? "Any object that implements the I interface"

3. Go in the interface:

Let's look at a few interface examples:

Copy Code code as follows:
Func SomeFunction (w interface{write (String)}) {
W.write ("pizza")
}

In this example, the interface is defined directly in the parameter, very special ...
Copy Code code as follows:
Func weirdfunc (i int) interface{} {
If i = = 0 {
Return "Zero"
}
return i;
}

In this example, because it is possible to return a string, it is possible to return int, so the return value is set to interface, which is seen extensively in the Go package package.

I hope this article will help you with your go language program.

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.