title: Accessing C array in golang
body: <p>I have two files, <code>module.go</code> and <code>test.py</code>. My goal is to speed up some calculations that is done in python, but have an issue accessing array of integers in go.</p>
  
  <p><code>module.go</code></p>
  
  <pre><code>package main
  
  import "C"
  
  //export Example
  func Example(testArray []C.int) C.int {
      return testArray[2]
  }
  
  func main() {}
  </code></pre>
  
  <p>and simple test file in python:</p>
  
  <pre><code>from ctypes import *
  
  # Load compiled go module
  lib = cdll.LoadLibrary("./gomodule.so")
  # We are passing an array of 256 elements and recieving integer
  lib.Example.argtypes = [c_int * 256]
  lib.Example.restype = c_int
  pyarr = [x for x in range(256)]
  # Make C array from py array
  arr = (c_int * len(pyarr))(*pyarr)
  print lib.Example(arr)
  </code></pre>
  
  <p>After compiling go module with <code>go build -buildmode=c-shared -o gomodule.so module.go</code> and fire up python file I got:</p>
  
  <pre><code>panic: runtime error: invalid memory address or nil pointer dereference
  [signal SIGSEGV: segmentation violation code=0x1 addr=0x12 pc=0x7fb18b6e688c]
  
  goroutine 17 [running, locked to thread]:
  main.Example(...)
          /home/metro/go/src/github.com/golubaca/carinago/module.go:7
  main._cgoexpwrap_53c1c00d0ad3_Example(0xa, 0x7fff33a2eac0, 0x7fff33a2ea70, 0x722a921de6cae100)
          _cgo_gotypes.go:47 +0x1c
  Aborted (core dumped)
  </code></pre>
  
  <p>I get that C array is different from Go, but can't find any tutorial how to access it's values without panic.</p>