0

I am trying out gomobile and want to send some data to a webserver before the app starts. I am using the basic example app template included in gomobile. I added the code at the start of main:

func main() {
    client := &http.Client{}
    req, _ := http.NewRequest("GET", "X.X.X.X:8000/log", strings.NewReader("TEST"))
    client.Do(req)
    app.Main(func(a app.App) {
                ...
        }
        ....
}

The app immediately crashes on start up. I am certain I used the correct IP in the GET request.
Is there anything wrong with how the HTTP request is made?
(I am testing on Android)

Bram Vanbilsen
  • 5,763
  • 12
  • 51
  • 84

1 Answers1

0

http.NewRequest likely returned an error because your URL doesn't include a scheme (http/https) and is thus invalid. Change "X.X.X.X:8000/log" to "http://X.X.X.X:8000/log".

Also you should handle errors, even if it's just a call to panic, as that will tell you what is wrong.

func main() {
    client := &http.Client{}
    req, err := http.NewRequest("GET", "http://X.X.X.X:8000/log", strings.NewReader("TEST"))
    if err != nil {
        panic(err)
    }

    _, err = client.Do(req)
    if err != nil {
        panic(err)
    }

    app.Main(func(a app.App) {
                ...
        }
        ....
}

Also, when testing on android, it is likely that your panics will end up in the system log that you can see with adb logcat (not 100% sure if that's the case for gomobile apps though).

xarantolus
  • 1,961
  • 1
  • 11
  • 19