skip to Main Content

I’ve got a variable which is equal to a pointer of the following struct:

type Conn struct {
  rwc io.ReadWriteCloser
  l   sync.Mutex
  buf *bytes.Buffer
}

Thus

fmt.Printf("---*cn: %+vn", *cn)

returns

{rwc:0xc42000e080 l:{state:0 sema:0} buf:0xc42005db20}

How can I see the value at the addresses 0xc42000e080 and 0xc42005db20?

My end goal is to inspect this because it’s used when connecting to memcache and on the chance memcache breaks I’m trying to reestablish connection, and need to inspect this to solve it.

2

Answers


  1. I am not sure why would you need a separate library for this. The straight forward answer is that you can directly dereference rwc inside the struct.

    So, you would do something like

    fmt.Printf("---*cn: %+vn", *(cn.rwc))
    
    Login or Signup to reply.
  2. Assuming Conn is imported and you don’t have access to the unexported fields with the standard selector expression you can use the reflect package to bypass that limitation.

    rv := reflect.ValueOf(cn)
    fmt.Println(rv.FieldByName("buf").Elem())
    

    https://play.golang.org/p/-6lWi1vYod

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search