blob: 57020e26ca76a2137d629b7809b86de5660c3fee [file] [log] [blame]
Pragya Arya324337e2020-02-20 14:35:08 +05301// Copyright 2017, The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE.md file.
4
5// +build !purego
6
7package cmp
8
9import (
10 "reflect"
11 "unsafe"
12)
13
14const supportExporters = true
15
16// retrieveUnexportedField uses unsafe to forcibly retrieve any field from
17// a struct such that the value has read-write permissions.
18//
19// The parent struct, v, must be addressable, while f must be a StructField
20// describing the field to retrieve.
21func retrieveUnexportedField(v reflect.Value, f reflect.StructField) reflect.Value {
22 // See https://github.com/google/go-cmp/issues/167 for discussion of the
23 // following expression.
24 return reflect.NewAt(f.Type, unsafe.Pointer(uintptr(unsafe.Pointer(v.UnsafeAddr()))+f.Offset)).Elem()
25}