package mian
import "C"
//export Min
func Min(a,b) int32{
if a>b{
return int32(b)
}
return int32(a)
}
编译命令
go build -ldflags "-s -w" -buildmode=c-shared -o net.dll main.go
package main
/*
#include <stdio.h>
#include <stdlib.h>
*/
import "C"
import (
"unsafe"
)
func main() {
for i := 1; i < 999; i++ {
cstr := C.CString("Hello, world")
C.free(unsafe.Pointer(cstr))//释放内存 必须引入stdlib.h 标准库
}
}
//返回文本//export Tostring
func Tostring(a *C.char) *int32{
text := C.GoString(a)
cstr := C.CString(text)
//C.free(unsafe.Pointer(cstr))
return (*int32)(unsafe.Pointer(cstr))
}
//export Getslice//传输字节集
func Getslice(CC **C.char,)int{
aa:="fdfdfsfdsfdsfdsfdsfsd"
zz:=[]byte(aa)
*CC=(*C.char)(unsafe.Pointer(C.CBytes(zz)))
return len(zz)
}