Why is the implementation of a string interface in go structure a struct rather than a pointer

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
Package Main//go The idea of a language combination is very heavy, between data if you want to inherit//need to combine the array in//go there is a concept of pointers, but there is no pointer operator import ("FMT") const (Animal_cat = 0animal_mouse = 1animal_dog = 2) interface of the//go language type Animal interface {name () (string) setName (name string) setType (animaltype int) GetType () ( int)}//cat type cat struct{//name m_name string "name _tag"//type M_type int "type _tag"//Kind M_spec string "kind _tag_ reflection with"}//receiver used (struct _name * structtype) and (Struct_name Structtype) are not the same//go language in the default all pass is copy//in use preference (Struct_name *structtype), avoid the value of func (cat CAT) name () (string) {return Cat.m_name}func (cat cat) SetName (name string) {Cat.m_name = Name}func (cat cat) SetType (ty int) {Cat.m_type = Ty}func (cat cat) GetType () (int) {return Cat.m_type}func (cat cat) Getspec () (string) {return cat.m_s Pec}func (Cat cat) show () {FMT. Printf (":=> name=%s type=%d spec=%s\n", Cat.m_name, Cat.m_type, Cat.m_spec)}//func (cat cat) init () {//cat.m_name = "C At "//cat.m_type = animal_cat//}//Tiger (cat branch) type tiger struct {//struct object cannot be used directly, but you can use the pointer//to aggregate a pointer of a cat object, which is equivalent to inheriting from CAT// The cat data is available, and the calling method is used directlyCat call//m_cat default is nil, all will automatically initialize M_cat * Cat}func main () {///No constructors, can construct struct to initialize cat: = cat{"Cat", Animal_cat, "Cat Spec"} Cat.show () fmt. Printf ("&cat=0x%x\n", &cat.m_name)//struct assignment operation, CAT4 and cat are two object cat4: = Catcat4.show () FMT respectively. Printf ("&cat4=0x%x\n", &cat.m_name)//If the receiver takes (Struct_name Structtype), the calling function cannot set the data for the struct///If the sink is using (struct _name* Structtype), there is no problem//go language is passed by default, not referenced, so if you want to modify the value, please wear a reference cat4.setname ("CAT1") Cat4.settype (Animal_dog) Cat4.show () cat.show ()//Use the New keyword to assign an object that is used directly when the go language initializes uninitialized data//access data. Instead of->CAT2: = new (cat) Cat2.m_name = " Mouse "Cat2.m_spec =" Mouse spec "Cat2.m_type = Animal_mousecat2.show () fmt. Printf ("&cat2=0x%x\n", CAT2)//Make assignment Operations Cat3 and CAT2 are two different pointers, but point to the same struct object cat3: = cat2fmt. Printf ("&cat3=0x%x\n", cat3) Cat3.m_name = "Mouse1" Cat3.show () Cat2.show ()}

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.