Advanced data types for Go learning notes

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Advanced data types, just to make a conceptual understanding, wait until other relevant knowledge of learning, then focus on analysis.

1 function

The language of function as a data type has many, core concepts of functional programming.

function is "First Class citizen", the function is equal to other data types, can be assigned to other variables, can be passed as a parameter to another function, or as a return value of another function.

In the Go language, the primary condition for assignment is the consistency of the type, and the premise that function assigns to other variables is that the function signature must be consistent to be considered the same type, otherwise the compilation error.

Here we need to introduce a new concept, the function signature (that is, the type signature of the function). A function signature is a function that does not contain a function body part, that is, the function's parameter type and the return value type. For example

Func func1 (a int) int {    //body}

The signature of the function is

FUNC1 (int) int//Can not include formal parameter names and return result names


For example

Func test_func_signature () {var var1 func (int) int Var var2 func (a int) int var var3 func (b int) int var1 = Func1 Var2 = Func1 var3 = func1 fmt. Printf ("%s", var1)//Three outputs consistent  FMT. Printf ("%s", VAR2)//Three outputs consistent  FMT. Printf ("%s", VAR3)//Three outputs consistent}func func1 (a int) int {return a}


Because the function itself is data, it can be composited into more complex data types like other data types, such as closures (nested function) and objects (custom types).

The method of the object, in the words of Rob Pike, is:

"A method is a function with an implicit first argument, called a receiver." (method is a function with the recipient)

Receiver has a restriction that cannot be a built-in base type, only a type-keyword-custom, but with one exception, custom interface cannot be used as receiver.

2 interface

The interface in the go language is not the same as the interface (interface) in other languages, as follows:

A, interface is also "first-class citizens", that is to say interface is actually a data type. That can be assigned to other variables, as parameters and as return values.

B, Interface is a collection of functions (signatures), not as the JAVA language is explicitly declared on a class that needs to implement interface, as long as a type is required to contain a collection of functions interface contains, it can be said that the type implements this interface.

C, interface because it is a collection of functions, that is, to be able to act as a function receiver type, and not as other languages are oriented class.

Interface type is assigned value, only the same type can be assigned, otherwise the error will be compiled (cannot use AAA as type BBB in Assignment), interface type how to confirm the same type?

As long as a collection of methods (functions) of a specific type contains a collection of methods (functions) specified by interface (must be signed consistently, the order does not matter), the acknowledgment type is the same.

Reference:

A preliminary study on functional programming-http://www.ruanyifeng.com/blog/2012/04/functional_programming.html

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.