当前位置:网站首页>CGO is realy Cool!
CGO is realy Cool!
2022-07-25 15:16:00 【Hua Weiyun】
- CGO is realy Cool!!
c/c++ in json and struct Mutual transformation is a troublesome problem , It's really not easy to deal with this problem with low intrusion ,cgo in go public c Structure definition of , stay go In the middle of json Operation and other subsequent businesses feel like a good idea .
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))} One thing to note cstruct The fields in are lowercase and go struct It will also be recognized as isUnexported, If it is changed to uppercase, it can be used normally in json See in string ;
If you don't want to change it cstruct Defined in the , Then you can put encode/json Copy it to the project directory and make customized modifications , modify encode.go Medium isUnexported by true, Even if the field is lowercase, you can see it in the output window ;
miao !
- Reference resources :
https://stackoverflow.com/questions/54101022/golang-struct-with-c-struct-in-cgo
边栏推荐
猜你喜欢

Application of object detection based on OpenCV and yolov3

npm的nexus私服 E401 E500错误处理记录

Outline and box shadow to achieve the highlight effect of contour fillet

防抖(debounce)和节流(throttle)

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

Yan required executor memory is above the max threshold (8192mb) of this cluster!

流程控制(上)

Spark AQE

Process control (Part 1)

System. Accessviolationexception: an attempt was made to read or write to protected memory. This usually indicates that other memory is corrupted
随机推荐
推荐10个堪称神器的学习网站
Scala110-combineByKey
Use the command to check the WiFi connection password under win10 system
oracle_12505错误解决方法
redis淘汰策列
Spark002 --- spark task submission, pass JSON as a parameter
Promise对象与宏任务、微任务
Iframe nested other website page full screen settings
给VS2010自动设置模板,加头注释
My creation anniversary
反射-笔记
outline和box-shadow实现外轮廓圆角高光效果
JS 同步、异步,宏任务、微任务概述
记一次Yarn Required executor memeory is above the max threshold(8192MB) of this cluster!
SQL Server forcibly disconnects
Overview of JS synchronous, asynchronous, macro task and micro task
基于OpenCV和YOLOv3的目标检测实例应用
Visual Studio 2022 查看类关系图
【微信小程序】小程序宿主环境详解
剑指Offer | 二进制中1的个数