Go language pointers

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

The go language retains the difference between the values in C and pointers, but it makes a lot of simplification for the cumbersome use of pointers, introducing the concept of references. So in the go language, you almost don't have to worry about getting all sorts of errors due to the direct manipulation of the inside inch. The go language pointer is basically left to distinguish between ByRef and ByVal semantics.

First, let's look at a simple example.

Package <textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;">Mainimport "FMT" func zeroval (ival int) {ival = 0}func zeroptr (iptr *int) {*iptr = 0}func main () {I : = 1 FMT. Println ("initial:", i) zeroval (i) fmt. Println ("Zeroval:", i) zeroptr (&i) fmt. Println ("Zeroptr:", i) pointers can be printed too. Fmt. PRINTLN ("pointer:", &i)}</textarea>
123456789101112131415161718192021 Package MainImport "FMT"funcZeroval(ivalint) {    ival = 0}funczeroptr(Iptr *int) {    *iptr = 0}funcMain() {    I := 1    FMT.Println("initial:", I)    Zeroval(I)    FMT.Println("Zeroval:", I)    zeroptr(&I)    FMT.Println("Zeroptr:", I)Pointerscan bePrintedtoo.    FMT.Println("pointer:", &I)}

Package <textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;">Mainimport "FMT" type Test struct{Name string}func change2 (t *test) {t.name = "2"}func change3 (t *test) { Note here the parentheses//if the direct *t.name=3 compilation does not pass an error invalid indirect of t.name (type String)//actually in go inside * can be omitted, directly similar to the CHANGE2 function in this use. (*t). Name = "3"}func change4 (t Test) {t.name = "4"}func main () {//t is an address t: = &test{name: "1"} FMT. Println (T.name); Change2 (t) fmt. Println (T.name); Change3 (t) fmt. Println (T.name); Here pass the variable with the * CHANGE4 (*t) fmt. Println (t.name);}</textarea>
1234567891011121314151617181920212223242526272829303132333435363738 Package MainImport "FMT"type Test struct{    Namestring}funcChange2(T *Test){    T.Name = "2"}funcChange3(T *Test){    //Note the parentheses here    //If direct *t.name=3 compilation does not pass error invalid indirect of t.name (type string)    //actually in go inside * can be omitted, directly similar to Change2 function in this use.     (*T).Name = "3"}funcChange4(T Test){    T.Name = "4"}funcMain() {    //t is an address    T := &Test{Name:"1"}    FMT.Println(T.Name);        Change2(T)    FMT.Println(T.Name);        Change3(T)    FMT.Println(T.Name);        //Here Pass the variable with *    Change4(*T)    FMT.Println(T.Name);}

Package <textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;">main Import "FMT" type ABC struct{v int} func (a ABC) AAAA () {//pass in the value instead of referencing A.v=1 FMT. Printf ("1:%d\n", A.V)} func (a *abc) bbbb () {//passed in is a reference, not a value of FMT. Printf ("2:%d\n", A.V) a.v=2 FMT. Printf ("3:%d\n", A.V)} func (a *abc) CCCC () {//passed in is a reference, not a value of FMT. Printf ("4:%d\n", A.V)} func main () {aobj:=abc{}//new (ABC); AOBJ.AAAA () aobj.bbbb () AOBJ.CCCC ()}</textarea>
1234567891011121314151617181920212223242526272829 Package Main Import "FMT" type ABC struct{    v int} func (a ABC)AAAA(){ //Pass in a value, not a reference    a.v=1    FMT.Printf("1:%d\n",a.v)} func (A *ABC)bbbb(){ //Incoming is a reference, not a value    FMT.Printf("2:%d\n",a.v)    a.v=2    FMT.Printf("3:%d\n",a.v)} func (A *ABC)CCCC(){ //Incoming is a reference, not a value    FMT.Printf("4:%d\n",a.v)} funcMain(){    Aobj:=ABC{}  //new (ABC);    Aobj.AAAA()    Aobj.bbbb()    Aobj.CCCC()}

Go language Pointers

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.