当前位置:网站首页>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
边栏推荐
- Run redis on docker to start in the form of configuration file, and the connection client reports an error: server closed the connection
- Leo-sam: tightly coupled laser inertial odometer with smoothing and mapping
- System.AccessViolationException: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏
- Maxcompute SQL 的查询结果条数受限1W
- MySql的安装配置超详细教程与简单的建库建表方法
- Bridge NF call ip6tables is an unknown key exception handling
- Example of password strength verification
- Implementation of asynchronous FIFO
- Spark SQL空值Null,NaN判断和处理
- Rediscluster setup and capacity expansion
猜你喜欢
随机推荐
Meanshift clustering-01 principle analysis
Spark DF增加一列
Recommend 10 learning websites that can be called artifact
MySQL installation and configuration super detailed tutorial and simple database and table building method
浏览器工作流程(简化)
Simulate setinterval timer with setTimeout
dpdk 收发包问题案例:使用不匹配的收发包函数触发的不收包问题定位
Understanding the execution order of T-SQL query from the execution order of join on and where
CMake指定OpenCV版本
RedisCluster搭建和扩容
Spark获取DataFrame中列的方式--col,$,column,apply
Spark-SQL UDF函数
Boosting之GBDT源码分析
【微信小程序】小程序宿主环境详解
Spark 内存管理机制 新版
Hbck 修复问题
Debounce and throttle
推荐10个堪称神器的学习网站
Rediscluster setup and capacity expansion
Introduction to raspberry Pie: initial settings of raspberry pie









