Skip to content

Instantly share code, notes, and snippets.

@graphaelli
Created October 28, 2018 03:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save graphaelli/4ccdf514624a4a0d173888e594fb4ed3 to your computer and use it in GitHub Desktop.
Save graphaelli/4ccdf514624a4a0d173888e594fb4ed3 to your computer and use it in GitHub Desktop.
// +build linux,cgo darwin,cgo
package main
import "C"
import (
"fmt"
"strconv"
)
func init() {
fmt.Println("loaded")
}
/*
fn maybe_panic(a: i32) -> String {
if a == 0 {
panic!("boom!")
} else {
a.to_string()
}
}
*/
//export maybe_panic
func maybe_panic(a int64) *C.char {
if a == 0 {
panic("boom!")
}
s := strconv.Itoa(int(a))
return C.CString(s)
}
func main() {}
// go build -buildmode=c-shared -o maybe_panic.so main.go
#!/usr/bin/env python
import ctypes
from ctypes import c_int, c_char_p
import sys
lib = ctypes.CDLL('maybe_panic.so')
maybe_panic = lib.maybe_panic
maybe_panic.argtypes = [c_int]
maybe_panic.restype = c_char_p
def main():
for arg in sys.argv[1:]:
res = maybe_panic(int(arg))
print(res)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment