在golang中导入OSX捆绑包?

huangapple go评论115阅读模式
英文:

Import OSX bundles in golang?

问题

在Python中,如果我想访问OSX bundle,我可以使用objc模块,像这样:

import objc
objc.loadBundle('CoreWLAN', 
           bundle_path='/System/Library/Frameworks/CoreWLAN.framework', 
           module_globals=globals())

在Golang中是否有一种优雅的方法来实现这个?也许可以使用一个模块吗?谷歌并没有给我找到太多相关信息。我是否需要使用语言的C导入功能来调用它?这是否可行?

我具体的情况是,我想读取可用接入点的WIFI数据,包括信道、信号强度和信噪比,以便与谷歌地图地理定位API进行通信。

英文:

So, in python if I want to access an OSX bundle I can use the objc module like this:

import objc
objc.loadBundle('CoreWLAN', 
       bundle_path='/System/Library/Frameworks/CoreWLAN.framework', 
       module_globals=globals())

Is there an elegant way to do this in golang, perhaps with a module? Google is not finding me much. Do I have to call out to this using the C import features of the language? Is this even possible?

My specific situation is that I would like to read WIFI data for available access points, channels, signal strength and signal/noise ratio for talking to the Google Maps Geolocation API.

答案1

得分: 2

Andlabs给出了一个很好的回答,但不幸的是他没有将其作为答案提交。

基本上,你可以像这样使用cgo...

//cgo LDFLAGS: -framework CoreWLAN

...然后你就能直接调用框架。引用他的回答:

>一个.framework包在其顶层包含一个动态加载的库。在系统框架的情况下,这个动态加载的库的文件名是框架的基本名称,并且没有典型的.dylib扩展名。如果你愿意直接使用libdl,也就是cgo,你可以动态加载这个dylib并直接调用它的函数。但是既然我们在这一点上正在使用cgo,有一个更好的方法://cgo LDFLAGS: -framework CoreWLAN,然后直接从cgo中使用API。– andlabs

>实际上,仔细研究一下,CoreWLAN的API是基于Objective-C的,所以你别无选择,只能使用cgo并执行我之前说的后一种方法 :/(不要尝试直接使用Objective-C运行时API,那会导致冗长、不安全和不可移植的代码。)– andlabs

英文:

Andlabs gave an excellent response that he has unfortunately not put in as an answer.

Basically you use cgo like this...

//cgo LDFLAGS: -framework CoreWLAN

...and you are able to call into the framework directly. To quote his responses:

>A .framework bundle contains at its top level a dynamically loaded library. In the case of the system frameworks, this dynamically loaded library's filename is the base name of the framework, and does not have the typical .dylib extension. If you are willing to use libdl directly, which means cgo, you can dynamically load this dylib and call its functions directly. But since we're using cgo at this point, there's a better way: //cgo LDFLAGS: -framework CoreWLAN and then use the API directly from cgo. – andlabs

>And in fact, looking into it, CoreWLAN's API is Objective-C based, so you have no choice but to use cgo and do the latter of what I said :/ (Do not try to use the Objective-C runtime API directly; that leads to verbose, unsafe, and nonportable code.) – andlabs

huangapple
  • 本文由 发表于 2015年9月16日 04:28:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/32595037.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定