cmd/getgo: determine current version via /dl/?mode=json API

The getgo tool was using the golang.org version as the latest
version to fetch. But this is not always the latest version.

We now use https://golang.org/dl/?mode=json which is the
official latest version.

Fixes golang/go#42676

Change-Id: I1cd90bfba12b19759599e89b7b4a095700999c09
Reviewed-on: https://go-review.googlesource.com/c/tools/+/270878
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Alexander Rakoczy <alex@golang.org>
Trust: Dmitri Shuralyov <dmitshur@golang.org>
Trust: Alexander Rakoczy <alex@golang.org>
Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
diff --git a/cmd/getgo/download.go b/cmd/getgo/download.go
index 1731131..86f0a2f 100644
--- a/cmd/getgo/download.go
+++ b/cmd/getgo/download.go
@@ -12,6 +12,7 @@
 	"archive/zip"
 	"compress/gzip"
 	"crypto/sha256"
+	"encoding/json"
 	"fmt"
 	"io"
 	"io/ioutil"
@@ -22,7 +23,6 @@
 )
 
 const (
-	currentVersionURL = "https://golang.org/VERSION?m=text"
 	downloadURLPrefix = "https://dl.google.com/go"
 )
 
@@ -168,18 +168,24 @@
 }
 
 func getLatestGoVersion() (string, error) {
-	resp, err := http.Get(currentVersionURL)
+	resp, err := http.Get("https://golang.org/dl/?mode=json")
 	if err != nil {
 		return "", fmt.Errorf("Getting current Go version failed: %v", err)
 	}
 	defer resp.Body.Close()
-	if resp.StatusCode > 299 {
+	if resp.StatusCode != http.StatusOK {
 		b, _ := ioutil.ReadAll(io.LimitReader(resp.Body, 1024))
-		return "", fmt.Errorf("Could not get current Go version: HTTP %d: %q", resp.StatusCode, b)
+		return "", fmt.Errorf("Could not get current Go release: HTTP %d: %q", resp.StatusCode, b)
 	}
-	version, err := ioutil.ReadAll(resp.Body)
+	var releases []struct {
+		Version string
+	}
+	err = json.NewDecoder(resp.Body).Decode(&releases)
 	if err != nil {
 		return "", err
 	}
-	return strings.TrimSpace(string(version)), nil
+	if len(releases) < 1 {
+		return "", fmt.Errorf("Could not get at least one Go release")
+	}
+	return releases[0].Version, nil
 }