草庐IT

types - 戈朗 : Export C fields to be externally visible using CGo

coder 2023-07-01 原文

背景:我正在尝试制作一个包,该包实质上是围绕我正在使用的 C 库提供精简的 Go 包装器。该包有意非常原始,因为其他几个包依赖于 C 库的低级功能,我不想复制粘贴一堆 Go 包装器代码。

假设我有一个如下所示的 C 结构:

typedef struct {
    uint32_t fizz;
    uint64_t buzz;
} test

在 CGo 中,我包装了 C 结构并创建了如下新方法:

package test    

type Test C.test

func NewTest() *Test {
    return &Test{1,2}
}

问题是在包之外,我无法访问 C-struct 中的字段

package main

import "test"

func main() {
    t := test.NewTest()
    _ = t.fizz // ERROR!!! Unexported field name!!
}

有什么简单的方法可以解决这个问题(除了为每个字段创建访问器方法之外)吗?

最佳答案

是的,您可以导出 C 结构。但是您需要遵循与导出 Golang 结构相同的规则来导出 C 结构。 http://golang.org/ref/spec#Exported_identifiers

main.go

package main

import "test"

func main() {
    t := test.NewTest()
    println(t.Fizz)
}

测试/测试.go

package test

/*
   #include "test.h"
*/
import "C"

type Test C.test

func NewTest() *Test {
    return &Test{Fizz: 1, Buzz: 2}
}

测试/测试.h

#include <stdint.h>

typedef struct {
    uint32_t Fizz;   // notice that the first character is upper case
    uint64_t Buzz;
} test;

如果出于某种原因您无法修改 C 结构中的字段名称,那么您将需要创建一个新的 C 结构来匹配确切的布局,但使用大写标识符。

关于types - 戈朗 : Export C fields to be externally visible using CGo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25217132/

有关types - 戈朗 : Export C fields to be externally visible using CGo的更多相关文章

  1. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  2. ruby-on-rails - Rails 单表继承 : How to override the value written to the type field - 2

    在我的系统中,我已经定义了STI。Dog继承自Animal,在animals表中有一个type列,其值为"Dog"。现在我想让SpecialDog继承自dog,只是为了在某些特殊情况下稍微修改一下行为。数据还是一样。我需要通过SpecialDog运行的所有查询,以返回数据库中类型为Dog的值。我的问题是因为我有一个type列,rails将WHERE"animals"."type"IN('SpecialDog')附加到我的查询中,所以我不能获取原始的Dog条目。所以我想要的是以某种方式覆盖rails在通过SpecialDog访问数据库时使用的值,使其表现得像Dog。有没有办法覆盖用于类型

  3. ruby-on-rails - 为什么方法 column_types 在 Rails 5.0 中未定义? - 2

    我正在为一个类赋值,它在rspec测试中使用了column_types方法。it"Userdatabasestructureinplace"doexpect(User.column_names).toinclude"password_digest","username"expect(User.column_types["username"].type).toeq:stringexpect(User.column_types["password_digest"].type).toeq:stringexpect(User.column_types["created_at"].type).t

  4. ruby - Formtastic,拥有 :as input type - 2

    如何将自己的字段类型添加到formtastic中?例如,我需要一个自定义的日期时间输入,我想要这样的东西::my_date%>这显然是行不通的,因为formtastic不知道:my_date(只有:boolean、:string、:datetime等等...)但是我怎样才能添加额外的输入类型呢? 最佳答案 您需要添加自定义输入法:classMyCustomFormtasticFormBuilder这非常适合新的HTML5输入类型。你可以这样使用它:MyCustomFormtasticFormBuilderdo|f|%>:my_dat

  5. ruby-on-rails - 我可以用鸭子类型(duck typing)改进这种方法吗? - 2

    希望我没有误解“ducktyping”的含义,但从我读到的内容来看,这意味着我应该根据对象如何响应方法而不是它是什么类型/类来编写代码。代码如下:defconvert_hash(hash)ifhash.keys.all?{|k|k.is_a?(Integer)}returnhashelsifhash.keys.all?{|k|k.is_a?(Property)}new_hash={}hash.each_pair{|k,v|new_hash[k.id]=v}returnnew_hashelseraise"CustomattributekeysshouldbeID'sorPropertyo

  6. ruby-on-rails - rails 不需要 Content-Type "application/json" - 2

    我在rails中有一个API端点,默认情况下,如果您没有设置任何Content-Typeheader,则会处理中的参数application/x-www-form-urlencoded有没有办法在不指定header中的内容类型的情况下处理来自POST请求的rails中的json字符串? 最佳答案 在您的routes.rb文件中,您可以将POST路由放置在命名空间中,并像这样定义预期的格式:namespace:api,defaults:{format::json}dopost'example'=>'controller#action'

  7. ruby-on-rails - 类型错误 : wrong argument type String (expected Module) - 2

    我有以下代码:classProfileLookup基本上包含大量查找数据,按类别拆分。目的是为数据库中的每个类别创建一个方法。通过Rails控制台,此代码按预期工作:ruby-1.9.3@hub:002>ProfileLookup.available_gendersProfileLookupLoad(0.6ms)SELECT"profile_lookups".*FROM"profile_lookups"WHERE"profile_lookups"."category"='gender'ORDERBYvalue=>["Female","Male"]但是,我的规范不合格。以下规范:requ

  8. ruby - 自定义 to_yaml 和 domain_type - 2

    我需要定义用于序列化/反序列化对象的自定义方法。我想做类似下面的事情。classPersondefto_yaml_type"!example.com,2010-11-30/Person"enddefto_yaml"stringrepresentingperson"enddeffrom_yaml(yaml)Person.load_from(yaml)endend声明序列化/反序列化的正确方法是什么? 最佳答案 好的,这就是我想出的classPersondefto_yaml_type"!example.com,2010-11-30/pe

  9. ruby - 如何在 ruby​​ 中设置 header ['content-type' ] ='application/json' - 2

    require'net/http'require'rubygems'require'json'url=URI.parse('http://www.xyxx/abc/pqr')resp=Net::HTTP.get_response(url)#get_responsetakesanURIobjectdata=resp.bodyputsdata这是我在ruby​​中的代码,resp.data以xml形式提供给我数据。restapi默认返回xml数据,如果headercontent-type是application/json,则返回json。但我想要json格式的数据。为此我必须设置heade

  10. ruby - pg gem : 'Warning: no type cast defined for type "numeric"' - 2

    我在从pggem中获取输入结果时遇到问题。require'pg'require_relative'spec/fixtures/database'client=PG.connect(DB[:pg])client.type_map_for_queries=PG::BasicTypeMapForQueries.new(client)client.type_map_for_results=PG::BasicTypeMapForResults.new(client)client.exec(%|select*fromtestme;|)do|query|query.each{|r|putsr.ins

随机推荐