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:
vim openurl.py
To run:
python openurl.py
Easy, right?!
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
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:
vim openurl.rs
To run:
rustc openurl.rs && ./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
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:
Almost a BASH! More easy than that just running directly at the prompt! 😛
Some people might ask:
Haha, just use system()
! 🍺