func main() { //Go string to C string s := "Hello Go string" cs := C.CString(s) C.print(cs) C.free(unsafe.Pointer(cs)) //C string to Go string fmt.Printf("%s\n", C.GoString(C.string)) }
func testSlice() { msg := "hello world!" p := C.calloc((C.size_t)(len(msg)+1), 1) println("malloc:", p) s := newSlice(p, len(msg)+1) copy(s.Data, []byte(msg)) fmt.Printf("fmt.Printf: %s\n", string(s.Data)) C.print((*C.char)(p)) }
func main() { fmt.Print("C.print: ") cs := C.CString("Hello from cgo\n") C.print(cs) C.free(unsafe.Pointer(cs)) fmt.Print("C.factorial: ") fmt.Println(C.factorial(5)) fmt.Print("C.gcd: ") fmt.Println(C.gcd(15, 230)) fmt.Print("C.prime: ") C.prime(6) fmt.Print("C.fibonacci: ") fmt.Println(C.fibonacci(5)) }
func C() { s := C.CString("Hello from C!\n") defer C.free(unsafe.Pointer(s)) C.print(s) }
func main() { s := "Hello Cgo" cs := C.CString(s) C.print(cs) C.free(unsafe.Pointer(cs)) }
func main() { var s = "hello cgo" csl := C.CBytes([]byte(s)) C.print(csl, C.int(len(s))) C.free(unsafe.Pointer(csl)) }
func main() { // s := "Hello Cgo" // cs := C.CString(s) C.print() }