How to open URL in default browser in: Go, Python, Ruby and Rust

Learn how to do this same task in 4 different programming languages.


How to open URL in default browser in: Go, Python, Ruby and Rust


Doing the same tasks in different programming languages improves our concept of logic. That is, the same result, but with different syntaxes.

In this article we will see how to open a URL in the default browser in 4 different programming languages:

Come on!


Python

In Python, just import the webbrowser library and use the open() function indicating the URL, for example:

vim openurl.py

import webbrowser
url="https://terminalroot.com/"
webbrowser.open(url)

To run: python openurl.py

Easy, right?!


Go

In Golang we could use the package: open-golang and use the commands: go mod init github.com/skratchdot/open-golang/open and ` go mod tidy` and compile.

But you can create a function for that, which will check the command on each different operating system(open, xdg-open and start), so the code would look like this:

vim openurl.go

package main

import (
  "fmt"
  "os/exec"
  "runtime"
  "log"
)

func openbrowser(url string) {
  var err error
  switch runtime.GOOS {
  case "linux":
    err = exec.Command("xdg-open", url).Start()
  case "windows":
    err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
  case "darwin":
    err = exec.Command("open", url).Start()
  default:
    err = fmt.Errorf("unsupported platform")
  }
  if err != nil {
    log.fatal(err)
  }
}

func main(){
  openbrowser("https://terminalroot.com/")
}

To run: go build openurl.go && ./openurl

Almost easy, shall we say! 😃


Rust

In Rust it is almost similar to the case of Go, that is, there is a crate that you can download and use or you can create a function and according to the operating system and run the process with the appropriate command, let’s do the second option:

Use std::process::Command!

For simplicity’s sake, let’s show you how to do this on UNIX-like systems:

vim openurl.rs

use std::process::Command;

fn main() {
    let url = "https://terminalroot.com/";
    let mut openurl = Command::new("xdg-open");
    openurl.arg(url);
    openurl.status().expect("process failed to execute");
}

To run: rustc openurl.rs && ./openurl

More or less easy too! 😎


Ruby

In Ruby it doesn’t run away from logic for Go and Rust. That is, we will use RbConfig::CONFIG['host_os'] to detect the operating system and create a case for the appropriate command. So:

vim openurl.rb

cmd = case RbConfig::CONFIG['host_os']
  when /mswin|mingw|cygwin/ then "start "
  when /darwin/ then "open "
  when /linux|bsd/ then "xdg-open "
  else raise "No OS detected"
end
    
b = system cmd + "https://terminalroot.com/"

To run: ruby openurl.rb

If it weren’t for the case it would be easier, for example on a UNIX-like system it was enough to run it via the command line:

ruby -e 'system "xdg-open " + "https://terminalroot.com/"'

Almost a BASH! More easy than that just running directly at the prompt! 😛

Some people might ask:

— And in C/C++ ?

Haha, just use system()! 🍺


Useful links


programming python go rust


Share


YouTube channel

Subscribe


Marcos Oliveira

Marcos Oliveira

Software developer
https://github.com/terroo

Related articles