如何通过Android上运行的Go程序访问互联网?

My program can't access to internet when put in android terminal, but in linux works fine. (wget (busybox) works fine with internet in android terminal)

package main

import (
    "io"
    "io/ioutil"
    "net/http"
)

func Url(url string)(string, io.ReadCloser, http.Header, error){
    var c = http.Client{}
    inf, err := c.Get(url)
    if err == nil {
        data,_ := ioutil.ReadAll(inf.Body)
        return string(data), inf.Body, inf.Header, err
    }
    return "", nil, nil, err
}

func main() {
    print("test internet... ")
    c,_,_,err := Url("http://ifconfig.me/ip")
    if err == nil {
        println("
 ip:", c)
    }else{
        println("error")
    }
}

compiling with:

go build main.go    # linux
CGO_ENABLED=0 GOOS=linux GOARCH=arm go build $(bin).go    # android

Android does not have full support for Go yet. If at some point in the future they add a Go sdk this will be easier. But for right now unless you feel like modifying Go's source yourself or writing your own set of libraries to handle Androids different environment this is going to be difficult to impossible.

Android does not have an /etc/resolv.conf

Somewhere in your code, or more likely one of those libraries you import, you are assuming a traditional linux userspace, which is not the case on Android.

maybe the "GOOS=linux" is the source of the mistake.