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!


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


import webbrowser

To run: python

Easy, right?!


In Golang we could use the package: open-golang and use the commands: go mod init 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 (

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()
    err = fmt.Errorf("unsupported platform")
  if err != nil {

func main(){

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

Almost easy, shall we say! 😃


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:


use std::process::Command;

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

To run: rustc && ./openurl

More or less easy too! 😎


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"
b = system cmd + ""

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 " + ""'

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