当前位置:网站首页>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,则就算字段小写也可以在输出窗口看到;
妙!
边栏推荐
- System.AccessViolationException: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏
- 43 box model
- Raft of distributed consistency protocol
- Leetcode combination sum + pruning
- Yarn: the file yarn.ps1 cannot be loaded because running scripts is prohibited on this system.
- Sudo rosdep init error ROS installation problem solution
- Bridge NF call ip6tables is an unknown key exception handling
- Scala111-map、flatten、flatMap
- IP address classification, which determines whether a network segment is a subnet supernetwork
- Detailed explanation of lio-sam operation process and code
猜你喜欢

【JS高级】js之正则相关函数以及正则对象_02

String type time comparison method with error string.compareto

oracle_ 12505 error resolution

Raft of distributed consistency protocol

Share a department design method that avoids recursion

45padding won't open the box

node学习

Deployment and simple use of PostgreSQL learning

瀑布流布局

Unable to start web server when Nacos starts
随机推荐
API health status self inspection
Object.prototype.hasOwnProperty() 和 in
"Ask every day" reentrantlock locks and unlocks
Detailed explanation of lio-sam operation process and code
[thread knowledge points] - spin lock
Sudo rosdep init error ROS installation problem solution
Introduction to raspberry Pie: initial settings of raspberry pie
Deployment and simple use of PostgreSQL learning
Content type corresponding to office file
Splice a field of the list set into a single string
解决asp.net上传文件时文件太大导致的错误
Copy files / folders through Robocopy
js URLEncode函数
万能通用智能JS表单验证
[C题目]力扣88. 合并两个有序数组
Sublimetext-win10 cursor following problem
IP address classification, which determines whether a network segment is a subnet supernetwork
海缆探测仪TSS350(一)
LeetCode第 303 场周赛
pkg_ Resources dynamic loading plug-in