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.
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:
In Python, just import the
webbrowser library and use the
open() function indicating the URL, for example:
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(
start), so the code would look like this:
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:
For simplicity’s sake, let’s show you how to do this on UNIX-like systems:
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:
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