英文:
Go /etc/resolv.conf missing in android
问题
我将我的Go源代码交叉编译为Arm架构,以在Android上运行。程序在Android设备的终端中运行,但是我收到一个错误,说/etc/resolv.conf文件丢失。我的程序在进行http调用时严重依赖于net/http库。
我该如何在Android中处理这个错误?
英文:
I cross compiled my Go source code to Arm to run in Android .The program runs in the terminal in the Android device, but I get an error saying that /etc/resolv.conf is missing. My program heavily relies on the net/http library in making http calls.
How can I handle this error in Android?
答案1
得分: 5
截至目前,你无法使用内置的net/http连接到主机名,可以查看这个问题(https://code.google.com/p/go/issues/detail?id=8020&q=%2Fetc%2Fresolv.conf&colspec=ID%20Status%20Stars%20Release%20Owner%20Repo%20Summary)。
有几种解决方法可以使用。
-
如果你有root访问权限,可以在程序启动时生成/etc/resolv.conf,虽然不太美观但简单易行。
-
将resolv.conf存储在某个地方,并修补http://golang.org/src/pkg/net/dnsclient_unix.go#L169以使用自定义路径。
-
修补
$GOROOT/src/pkg/net/dnsconfig_unix.go
以通过执行getprop net.dns1
和getprop net.dns2
获取名称服务器。 -
实现完整的修补程序,参考https://gist.github.com/ernesto-jimenez/8042366。
英文:
As of right now you can't use the built in net/http to connect to hostnames, check this issue.
There are few workarounds you could use.
-
generate /etc/resolv.conf on your program start if you have root access, ugly but easy and simple.
-
store resolv.conf somewhere and patch http://golang.org/src/pkg/net/dnsclient_unix.go#L169 to use that custom path.
-
patch
$GOROOT/src/pkg/net/dnsconfig_unix.go
to get the nameservers by executinggetprop net.dns1
andgetprop net.dns2
. -
implement the full patch at https://gist.github.com/ernesto-jimenez/8042366
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论