当前位置:网站首页>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,则就算字段小写也可以在输出窗口看到;
妙!
边栏推荐
猜你喜欢

bridge-nf-call-ip6tables is an unknown key异常处理

oracle_12505错误解决方法

在win10系统下使用命令查看WiFi连接密码

图片裁剪cropper 示例

Visual Studio 2022 查看类关系图

System. Accessviolationexception: an attempt was made to read or write to protected memory. This usually indicates that other memory is corrupted

6月产品升级观察站

39 simple version of millet sidebar exercise

用setTimeout模拟setInterval定时器

44 Sina navigation, Xiaomi sidebar exercise
随机推荐
Visual Studio 2022 查看类关系图
How many ways can you assign initial values to a two-dimensional array?
27 选择器的分类
(original) customize a scrolling recyclerview
35 快速格式化代码
32 use of chrome debugging tools
MySQL sort
39 简洁版小米侧边栏练习
MySQL之事务与MVCC
Promise对象与宏任务、微任务
PHP implements non blocking (concurrent) request mode through native curl
LeetCode_字符串_中等_151.颠倒字符串中的单词
Gbdt source code analysis of boosting
用setTimeout模拟setInterval定时器
35 quick format code
如何解决Visual Stuido2019 30天体验期过后的登陆问题
SQL Server forcibly disconnects
41 picture background synthesis - colorful navigation map
流程控制(上)
[C topic] the penultimate node in the Niuke linked list