Go language Implementation interface, the recipient should be a value or a reference (reference-compatible pass value)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
When assigning a value to an interface in a/*go language, if the object is a value (not handled by the recipient of the reference), the value can be handled automatically if it is a pointer */package mainimport "FMT"//Defines the integer type integer inttype Lessaddinf interface{less (n integer) booladd (n integer) integer}func (this integer) less (n integers) Bool{return this < N}func (This *integer) ADD (n Integer) integer{*this + = Nreturn *this}type computer struct{cpu string "Calculator" Memory string "inside Save "}type Thing Interface{name () Stringattribute () String}func (this computer) Name () string {return ' computer '}func (this *computer) Attribute () string {return FMT. Sprintf ("Cpu=%v memory=%v", this. CPU, this. Memory)}func Main () {var inf lessaddinfvar n integerinf = &nfmt. Printf ("INF. Less (=%v\n) ", INF. Less (+)) fmt. Printf ("INF. ADD (+) =%v\n ", INF. ADD ()) var thing thingvar computer = computer{cpu: "Intel Xeon-v3440", Memory: "Samsung DDR4 (8g)"}thing = &computerfmt. Printf ("thing. Name () =%v\n ", thing. Name ()) fmt. Printf ("thing. Attribute () =%v\n ", thing. Attribute ())}

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.