如何在Golang中解开python对象

I have a python program wherein I am using Pickle to store the object using the following:

pickle.dump(sample, open( "Pickled_files/sample.p", "wb" )) 

I can extract and unpickle this object in Python using the following:

sample_extracted= pickle.load(open( "Pickled_files/sample.p", "rb" ))

However, I need to extract this object in a Golang application. Thus I need to know a way by which objects pickled using Python are extracted in Golang.

Is there a way that this can be achieved? And if yes, I would really appreciate if someone can point me to a sample reference or example.

Pickle is Python specific format. AFAIK there are no pickle-parsers outside of Python. You can try to write one for Go but you will most likely only waste lots of time and mental health. On the other hand that would be an interesting project, indeed.

Anyway, instead of pickling use any language independent format, i.e. xml, json, google's protobuf or even a custom one, whatever suits your needs. Always pick tool for a job, never other way around.

Is there a way that this can be achieved?

Depends on your understanding of this. There are a lot of better options than Pickle -- even in pure Python environments. If you understand this as exchanging data between golang and Python, you should consider the following example:

You can serialize everything in Python, like

import msgpack
import msgpack_numpy as m
m.patch()
import numpy as np

data = {'string': 'Hello World',
        'number': 42,
        'matrix': np.random.randn(2, 3)}

with open('data.msgp', 'wb') as f:
    f.write(msgpack.packb(data, use_bin_type=True))

Reading it is pretty simple

// go get -u github.com/vmihailenco/msgpack

package main

import (
    "fmt"
    "github.com/vmihailenco/msgpack"
    "io/ioutil"
)

func main() {

    buf, err := ioutil.ReadFile("data.msgp")
    if err != nil {
        panic(err)
    }

    var out map[string]interface{}
    err = msgpack.Unmarshal(buf, &out)
    if err != nil {
        panic(err)
    }

    for k, v := range out {
        fmt.Printf("key[%v] value[%v]
", k, v)
    }
}

This gives you

key[matrix] value[map[data:[145 106 174 12 61 187 235 63 128 225 138 214 167 154 231 191 156 205 144 51 50 116 244 191 251 147 235 33 187 149 251 63 207 56 134 174 206 146 220 63 7 23 246 148 34 235 226 63] type:[60 102 56] 
            kind:[] nd:true shape:[2 3]]]
key[string] value[[72 101 108 108 111 32 87 111 114 108 100]]
key[number] value[42]

All is left is converting the byte sequences into the object you would like to have.

ogórek (2) is Go library for decoding/encoding Python pickles.

A pickle saved to the file can be loaded in Go as follows:

f, err := os.Open("Pickled_files/sample.p")

d := ogórek.NewDecoder(f)
obj, err := d.Decode()