Go language Perverted usage

Source: Internet
Author: User
This is a created article in which the information may have evolved or changed.

Today in order to submit notepad++ highlighted bug, wrote a perverted code.

Bug Address: https://code.google.com/p/go/issues/detail?id=5798

The following is a slightly adjusted code (HTTP://PLAY.GOLANG.ORG/P/RYQ82B7BN2):

package mainimport "fmt"type int bytefunc (p int) Foo() {    fmt.Printf("int.Foo: %v\n", p)}func int_Foo(x byte) {    f1 := int.Foo    f1(int(x))    f2 := int(x).Foo    f2()}func main() {    int_Foo(32)}

Code output:

int.Foo: 32int.Foo: 32

The main features are as follows:

  • The built-in type is redefined int and is defined as a type and can be defined as a variable name.
  • intmethod expression for the new type: expressionsint.Foo
  • intmethod value for the new type expressions:int(x).Foo
  • cannot be defined recursively as its own type, such as type int int . Causes an invalid recursive type error: "Invalid recursive type int".
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.