当前位置:网站首页>CGO is realy Cool!
CGO is realy Cool!
2022-07-25 15:10:00 【华为云】
- CGO is realy Cool!!
c/c++中json和struct互转是一个比较麻烦的问题,想要用低侵入的手段处理这个问题确实不是很容易,cgo中go公用c的结构体定义,在go中进行json操作和后续的其他业务感觉是个不错的注意.
package main//#cgo CFLAGS: -I /code/xxx/include//#include "xxx.h"/*typedef struct Point { int x , y;} Point;struct struct_xxx xxx;*/import "C"import ( "fmt" "log" "nep_go_st/json")type CPoint struct { Point C.Point `json:"point"`}func main() { point := C.Point{x: 1, y: 2} ret1, err := json.Marshal(point) if err != nil { log.Fatal(err) } fmt.Println(string(ret1)) ret1, err = json.Marshal(C.xxx) if err != nil { log.Fatal(err) } fmt.Println(string(ret1))}需要注意一点cstruct中的字段小写和go struct里面一样会被识别为isUnexported,修改为大写则可以正常在json字符串中看到;
如果不想修改cstruct中的定义, 则可以将encode/json拷贝到项目目录做一下定制修改,修改encode.go中的isUnexported为true,则就算字段小写也可以在输出窗口看到;
妙!
边栏推荐
- [thread knowledge points] - spin lock
- [C题目]力扣876. 链表的中间结点
- Solve the error caused by too large file when uploading file by asp.net
- Yarn: the file yarn.ps1 cannot be loaded because running scripts is prohibited on this system.
- 39 简洁版小米侧边栏练习
- ES5写继承的思路
- Pl/sql creates and executes ORALCE stored procedures and returns the result set
- Scala110-combineByKey
- 45padding won't open the box
- 27 classification of selectors
猜你喜欢

I2C device driver hierarchy

node学习

String type time comparison method with error string.compareto

用OpenPose进行单个或多个人体姿态估计

"Ask every day" reentrantlock locks and unlocks

Gbdt source code analysis of boosting

Unable to start web server when Nacos starts

39 简洁版小米侧边栏练习

ESXI6.7.0 升级到7.0U3f(2022年7月12 更新)

用setTimeout模拟setInterval定时器
随机推荐
bridge-nf-call-ip6tables is an unknown key异常处理
剑指Offer | 二进制中1的个数
MySQL sort
Fast-lio: fast and robust laser inertial odometer based on tightly coupled IEKF
一个程序最多可以使用多少内存?
45padding won't open the box
[C题目]牛客 链表中倒数第k个结点
流程控制(上)
TypeScript学习2——接口
[C topic] force buckle 876. Intermediate node of linked list
[comprehensive pen test] difficulty 4/5, classic application of line segment tree for character processing
Thymeleaf notes
什么是物联网
"Ask every day" briefly talk about JMM / talk about your understanding of JMM
如何解决Visual Stuido2019 30天体验期过后的登陆问题
"Ask every day" reentrantlock locks and unlocks
Raft of distributed consistency protocol
浏览器工作流程(简化)
Handle Oracle deadlock
CMake指定OpenCV版本