英文:
Accessing Linux network APIs from Go
问题
我想用Go语言编写一个简单的实用程序来管理Linux上的网络(有线和无线)连接,类似于NetworkManager和WICD的软件。
在Go中,访问Linux API的正确方式是什么?我应该使用C绑定和本地Linux API调用,执行命令并解析其输出,还是可能有一些专门设计用于实现我想要的功能的库?
英文:
I would like to write a simple utility program in Go to manage network (wired and wireless) connections on Linux, a software similar to NetworkManager and WICD.
What would be the correct way to access Linux APIs in Go? Should I use C bindings and native Linux API call, execute a command and parse its output or maybe there is some library designed to do what I want to do?
答案1
得分: 1
你很可能想使用cgo,因为它非常容易调用任何C API。
另一个好处是,C API通常在很长一段时间内都非常稳定,因为库的创建者几乎总是选择添加新的函数而不是破坏现有函数的API。
运行命令行工具并解析输出是容易出错的,因为你调用的软件很可能会随着时间的推移改变其输出,从而破坏你的解析器。
英文:
You very likely want to use cgo as it's really easy to call any C API
An added benefit is that C APIs are usually pretty stable over time as the library creators almost always opt for a new function rather than break the API of an existing one.
Running command line tools and parsing output is error prone as the software you call will most likely change it's output overtime, breaking your parsers.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论