Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3947814
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:17:48+00:00 2026-05-20T01:17:48+00:00

I am just playing around with Go and do not yet have a good

  • 0

I am just playing around with Go and do not yet have a good mental model of when structs are passed by value or by reference.

This may be a very dumb question but I just want to experiment a bit and see if I am still working on the same object or I have made a copy of it (passed it by value).

Is there a way to print the pointer (or internal id if pointer value is changed by gc) of an object?

package main

import ( "runtime" )

type Something struct {
    number int
    queue chan int
}

func gotest( s *Something, done chan bool ) {
    println( "from gotest:")
    println( &s )
    for num := range s.queue {
        println( num )
        s.number = num
    }
    done <- true
}

func main() {
    runtime.GOMAXPROCS(4)
    s := new(Something)
    println(&s)
    s.queue = make(chan int)
    done := make(chan bool)
    go gotest(s, done)
    s.queue <- 42
    close(s.queue)
    <- done
    println(&s)
    println(s.number)
}

gives on my windows (8g compiled version):

0x4930d4
from gotest:
0x4974d8
42
0x4930d4
42

Why does the pointer value from within the go routine show a different value? The quantity on the original object did get changed so it was working with the same object. Is there a way to see an object id that is persistent?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T01:17:48+00:00Added an answer on May 20, 2026 at 1:17 am

    Go function arguments are passed by value.

    First, let’s discard the irrelevant parts of your example, so that we can easily see that you are merely passing an argument by value. For example,

    package main
    
    import "fmt"
    
    func byval(q *int) {
        fmt.Printf("3. byval -- q %T: &q=%p q=&i=%p  *q=i=%v\n", q, &q, q, *q)
        *q = 4143
        fmt.Printf("4. byval -- q %T: &q=%p q=&i=%p  *q=i=%v\n", q, &q, q, *q)
        q = nil
    }
    
    func main() {
        i := int(42)
        fmt.Printf("1. main  -- i  %T: &i=%p i=%v\n", i, &i, i)
        p := &i
        fmt.Printf("2. main  -- p %T: &p=%p p=&i=%p  *p=i=%v\n", p, &p, p, *p)
        byval(p)
        fmt.Printf("5. main  -- p %T: &p=%p p=&i=%p  *p=i=%v\n", p, &p, p, *p)
        fmt.Printf("6. main  -- i  %T: &i=%p i=%v\n", i, &i, i)
    }
    

    Output:

    1. main  -- i  int: &i=0xf840000040 i=42
    2. main  -- p *int: &p=0xf8400000f0 p=&i=0xf840000040  *p=i=42
    3. byval -- q *int: &q=0xf8400000d8 q=&i=0xf840000040  *q=i=42
    4. byval -- q *int: &q=0xf8400000d8 q=&i=0xf840000040  *q=i=4143
    5. main  -- p *int: &p=0xf8400000f0 p=&i=0xf840000040  *p=i=4143
    6. main  -- i  int: &i=0xf840000040 i=4143
    

    In function main, i is an int variable at memory location (&i) 0xf800000040 with an initial value (i) 42.

    In function main, p is a pointer to an int variable at memory location (&p) 0xf8000000f0 with a value (p=&i) 0xf800000040 which points to an int value (*p=i) 42.

    In function main, byval(p) is a function call which assigns the value (p=&i) 0xf800000040 of the argument at memory location (&p) 0xf8000000f0 to the function byval parameter q at memory location (&q) 0xf8000000d8. In other words, memory is allocated for the byval parameter q and the value of the main byval argument p is assigned to it; the values of p and q are initially the same, but the variables p and q are distinct.

    In function byval, using pointer q (*int), which is a copy of pointer p (*int), integer *q (i) is set to a new int value 4143. At the end before returning. the pointer q is set to nil (zero value), which has no effect on p since q is a copy.

    In function main, p is a pointer to an int variable at memory location (&p) 0xf8000000f0 with a value (p=&i) 0xf800000040 which points to a new int value (*p=i) 4143.

    In function main, i is an int variable at memory location (&i) 0xf800000040 with a final value (i) 4143.

    In your example, the function main variable s used as an argument to the function gotest call is not the same as the function gotest parameter s. They have the same name, but are different variables with different scopes and memory locations. The function parameter s hides the function call argument s. That’s why in my example, I named the argument and parameter variables p and q respectively to emphasize the difference.

    In your example, (&s) 0x4930d4 is the address of the memory location for the variable s in function main that is used as an argument to the function call gotest(s, done), and 0x4974d8 is the address of the memory location for the function gotest parameter s. If you set parameter s = nil at the end of function gotest, it has no effect on variable s in main; s in main and s in gotest are distinct memory locations. In terms of types, &s is **Something, s is *Something, and *s is Something. &s is a pointer to (address of memory location) s, which is a pointer to (address of memory location) an anonymous variable of type Something. In terms of values, main.&s != gotest.&s, main.s == gotest.s, main.*s == gotest.*s, and main.s.number == gotest.s.number.

    You should take mkb’s sage advice and stop using println(&s). Use the fmt package, for example,

    fmt.Printf("%v %p %v\n", &s, s, *s)
    

    Pointers have the same value when they point to the same memory location; pointers have different values when they point to different memory locations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just playing around with jqGrid this afternoon, and have it working fairly well
Was just playing around with nodejs and chrome's console when I tested this: []
I'm just playing around with WPF and MVVM, and I have made a simple
I'm just playing around with FastMember and have hit a problem. Each time I
I am just playing around with threads in java. I have a class which
I'm just playing around with some code but it seemed to have the specific
Just playing around with interfaces and I have a question about something which I
I'm a very new to C#. Just playing around with it. Not for a
I was just playing around with CSS positioning and I have got a few
Hey guys, I'm just playing around with SQLite so this is new for me.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.