github.com/wencode/hack@v0.2.9/example/hello.go (about) 1 package main 2 3 import "C" 4 import ( 5 "runtime" 6 "log" 7 "unsafe" 8 9 "github.com/wencode/hack/dl" 10 "github.com/wencode/hack/proc" 11 ) 12 13 var ( 14 libcfile string 15 ) 16 17 func init() { 18 switch runtime.GOOS { 19 case "darwin": 20 libcfile = "/usr/lib/libc.dylib" 21 case "windows": 22 default: 23 libcfile = "libc.so.6" 24 } 25 26 } 27 28 29 func main() { 30 lib, err := dl.Open(libcfile) 31 if err != nil { 32 log.Fatal(err) 33 } 34 defer lib.Close() 35 write_addr := lib.Sym("write") 36 if write_addr == 0 { 37 log.Fatalf("can't find printf symbol") 38 } 39 40 str := []byte("hello,world\n") 41 l := proc.Call(write_addr, 1, uintptr(unsafe.Pointer(&(str[0]))), uintptr(len(str))) 42 if l != len(str) { 43 log.Fatal("call write failed") 44 } 45 }