我爱占星网 我爱占星网
首页
编程
java
php
前端
首页 编程 java php 前端

如何通过golang中的CGO将Go字符串复制到C char *?

I want to copy a Go string into a char * via CGO.

Am I allowed to do this something like this?

func copy_string(cstr *C.char) {

    str := "foo"
    C.GoString(cstr) = str

}

According to the cgo documentation you need to use the C.CString function to convert a Go string to a C string:

cstr = C.CString(str)

Be aware that C.CString function allocates the memory for you, but won't release it, so it is your responsability to freed the memory with a call like:

C.free(unsafe.Pointer(cstr))

近期文章

  • 使用AES 256 CTR在Node JS中加密和在Golang中解密
  • Golang:过程花了太长时间。 实施拼写检查器
  • 在Go中高效读写CSV
  • 使Go Webapp独立于Google App Engine
  • Golang重命名Unicode文件名
  • 如何在Go中保持强大的参考力?
  • 在Go中,如何获取结构的字符串表示形式?
  • 使用Go检索MongoDB文档的问题
  • Golang:为什么response.Get(“ headerkey”)在此代码中未返回值?
  • Go mgo.v2软件包错误:没有可访问的服务器
  • 在Go中使用分叉包导入
  • 服务器上未启用SSL
  • 正则表达式将字符串解析为环境变量
  • 如何在Golang中间接传递接口
  • 为什么我的golang程序会创建这么多线程?
  • ReadString()是否在换行符后丢弃字节?
  • 解组JSON HTTP响应
  • Loadrunner action错误
  • Go代码执行Redis操作时缺少毫秒
  • Alice’s Cube 代码怎么设计的

Copyright ©2022 我爱占星 All Rights Reserved.

浙ICP备2022030071号-1

部分图文来自网络,如有侵犯您的版权,请告诉我们删除

友情链接:代码精华