dpiz9879 2019-02-25 09:11
浏览 44

在Go和Windows API中处理大小不一的数组

What is the idiomatic way to deal with unsized arrays in Go? I'm working on the ETW wrappers and the TdhGetEventInformation function fills in the provided memory buffer with event information. The event metadata is represented by TRACE_EVENT_INFO structure, which has an array member declared as:

EVENT_PROPERTY_INFO EventPropertyInfoArray[ANYSIZE_ARRAY];

I'm calling the TdhGetEventInformation function in a way that the provided buffer has enough space to populate event properties array:

var bufferSize uint32 = 4096
buffer := make([]byte, bufferSize)

tdhGetEventInformation.Call(
   uintptr(unsafe.Pointer(eventRecord)),
   0, 0, 
   uintptr(unsafe.Pointer(&buffer[0])),
   uintptr(unsafe.Pointer(&bufferSize)),
)

However, since I'm tempting to model the Go counterpart struct with EventPropertyInfoArray field as

EventPropertyInfoArray [1]EventPropertyInfo

the compiler is not able to re dimension the array according to the number of available properties for each event, so I end up with one-array item.

Do you have any smart ideas on how handle this edge case?

Thanks in advance

  • 写回答

2条回答 默认 最新

  • dongwen5351 2019-02-25 20:44
    关注

    So you want a variable sized array in Go? Use a slice?

    EventPropertyInfoArray [1]EventPropertyInfo
    

    Would be

    EventPropertyInfoArray []EventPropertyInfo
    

    If you have a rough idea of a maximum it could hold you could make an array using make something like this, but this wouldn't help you out in declaring a struct:

    EventPropertyInfoArray = make([]EventPropertyInfo, len, capacity)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看