Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,28 @@
|
|||
package main
|
||||
|
||||
// #include <string.h>
|
||||
// #include <stdlib.h>
|
||||
import "C"
|
||||
import (
|
||||
"fmt"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// a go string
|
||||
go1 := "hello C"
|
||||
// allocate in C and convert from Go representation to C representation
|
||||
c1 := C.CString(go1)
|
||||
// go string can now be garbage collected
|
||||
go1 = ""
|
||||
// strdup, per task. this calls the function in the C library.
|
||||
c2 := C.strdup(c1)
|
||||
// free the source C string. again, this is free() in the C library.
|
||||
C.free(unsafe.Pointer(c1))
|
||||
// create a new Go string from the C copy
|
||||
go2 := C.GoString(c2)
|
||||
// free the C copy
|
||||
C.free(unsafe.Pointer(c2))
|
||||
// demonstrate we have string contents intact
|
||||
fmt.Println(go2)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue