The difference between common functions and methods in go language

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

1. For normal functions, when the recipient is a value type, the data of the pointer type cannot be passed directly, and vice versa.

2. For methods such as the method of a struct, when the recipient is a value type, you can call the method directly with a variable of pointer type, and vice versa.

The following is a simple example:

Package structtest//the difference between a normal function and a method (when the recipient is a value type and a pointer type)//date:2014-4-3 10:00:07import ("FMT") func structtest06base () { structTest0601 () structTest0602 ()}//1. function of the normal function//Receive value type parameter func valueinttest (a int) int {return a + 10}//receive pointer type argument function func Pointerinttest (a *int) int {return *a + 10}func structTest0601 () {A: = 2fmt. Println ("Valueinttest:", Valueinttest (a))//The parameter of the function is a value type, you cannot pass the pointer directly as a parameter//fmt. Println ("Valueinttest:", Valueinttest (&a))//compile error:cannot use &a (type *int) as type int in function Argum ENTB: = 5fmt. Println ("Pointerinttest:", Pointerinttest (&B))///Similarly, when a function's argument is a pointer type, the value type cannot be passed directly as a parameter//fmt. Println ("Pointerinttest:", Pointerinttest (b))//compile Error:cannot use B (Type int.) as type *int in function argument}//2 . method type Persond struct {ID intname string}//receiver is a value type func (P persond) Valueshowname () {fmt. The Println (p.name)}//recipient is a pointer-type func (P-*persond) Pointshowname () {fmt. Println (p.name)}func structTest0602 () {//value type Call method Personvalue: = persond{101, "'ll Smith"}personvalue.valueshowname () PersoNvalue.pointshowname ()//pointer type call method Personpointer: = &persond{102, "Paul Tony"}personpointer.valueshowname () Personpointer.pointshowname ()//Unlike normal functions, the recipient is a method of pointer type and value type, and variables of pointer type and value type can be called each other}


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.