0

Caveat: Both this little bash line and the question here are for learning purposes, as I realize that I could "just open a browser".

Unless I am mistaken, Google does not provide a script-friendly way to download the latest version of golang. Scripts I have seen (such as this gist) still require you to know and to manually input the go version number.

My goal was to see if I could automatically retrieve the latest version without looking it up.

My solution does work but I would love to learn a leaner and cleaner way :

GOURL=$(curl https://golang.org/dl/ 2>&1 | grep -o -E 'href="([^"#]+)"' | cut -d'"' -f2 | awk '/tar.gz/' | awk NR==1)

In a nutshell, it gets all links from golang's download page, cleans up the urls to only keep the tar.gz ones, and finally retains the first line of the output which is the exact link I would need. Right now: it would be for go.1.8.3.

Once again, this is for learning purposes. Thanks for your time.

EDIT: I 'accepted' GMaster's suggestion as the answer as it improved my solution with no additional tools. I am grateful to other commenters however for helping me discover extra tools. Although I eagerly look forward to any possible additions in the future, I thought it best to close this since my request was amply addressed.

3 Answers 3

2

If you look at the https://golang.org/dl/ page, you will see the latest versions are in Featured Downloads. Now, assuming Google does not change the html structure of the page, this is a cleaner version

GOURL=$(curl -s https://golang.org/dl/ | grep 'download.*downloadBox' | grep -o 'https.*linux.*gz')
2
  • If you can use Pup: curl https://golang.org/dl/ -s | pup 'a.downloadBox:parent-of(div:contains("Linux")) attr{href}' (Or pup 'a.downloadBox[href*="linux"] attr{href}')
    – muru
    Jun 1, 2017 at 7:02
  • Thanks for this. I had jq installed but this is a step up from that!
    – farhang
    Jun 1, 2017 at 15:28
2

Elegance is mostly subjective. The easy way to keep an eye on Go releases is to set a watch at the releases page at GitHub, or just add the relevant feed to your news feeds (assuming you're using RSS feeds, that is).

But if you must have a command that looks more clever than curl + grep, here's one using Xidel:

{ echo -n url=; xidel https://golang.org/dl/ -s -e '//a[./div="Source"]/@href'; } | \
    curl -s -S -O -K -

Here xidel extracts the relevant URL from the download page using XPath, then curl downloads it. Probably way overkill here, but xidel might come in handy for other tasks.

2
  • Thank you for this tip. I just downloaded xidel and I look forward to learning to use it. I certainly don't mind overkill when it comes to learning!
    – farhang
    Jun 1, 2017 at 15:33
  • @Farhang The point of using xidel (and pup, as pointed out in the comment to the other answer) is to parse the structure of the page to locate the relevant URL. In principle that's safer than grep because grep could be fooled to match other parts of the page, especially as the page changes over time. But in this particular case it's a very simple page, grep should likely keep working just fine. Jun 1, 2017 at 16:28
1

No need for a Bash script. xidel is all you need to parse the golang website.
And no need for curl either if you want to download the file.

To extract the url of the latest golang's source:

$ xidel -s "https://golang.org/dl" -e '//a[div="Source"]/resolve-uri(@href)'
#or
$ xidel -s "https://golang.org/dl" -e 'resolve-uri(//tr[td="Source"]/td/a/@href[1])'
https://golang.org/dl/go1.16.5.src.tar.gz

You could of course download this url with curl...

$ curl -LO "$(xidel -s "https://golang.org/dl" -e '//a[div="Source"]/resolve-uri(@href)')"

...but xidel can do that too...

$ xidel -s "https://golang.org/dl" -f '//a[div="Source"]/resolve-uri(@href)' --download '.'

...which downloads 'go1.16.5.src.tar.gz' to the current dir.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .