My first time using GoLang as a PHP Developer

Elijah Cruz
5 min readJun 25, 2021

Golang. It seemed like something that looked very interesting but seemed like it would be very difficult to grasp. Looking at various tutorials, its website, and even its “tour” meant to teach you how to use it, it still seemed very difficult. I tried it, and for 1 year, didn’t touch it again. Until now.

Recently I had moved from California to Texas, and as of writing this, I am still staying in a motel until I finally get an apartment out here. The motel’s wifi is absolutely terrible, and sometimes even when it says I’m connected, nothing will actually load. I decided there has to be a way for me to be able to tell if I’m connected or not, without waiting 30+ seconds for a page to tell me that it just isn’t wanting to work. I wanted it to be terminal-based, have the ability to let others try it and use it, and didn’t require someone to download other packages or the programming language itself (like PHP) to use it.

Now, for this, there are many languages that probably would’ve worked well. C, C++, Python, and many others might’ve worked, but I went poking around the internet and it brought me back to GoLang, for various reasons.

For one, GoLang is a static language, meaning that it can be compiled. This would allow me to bundle it up into a script that I can run on the command line. Two, it includes a VERY simple HTTP client build right into the program. This means that I could add 1–2 lines of code and already be able to check the internet. Three, if I end up making a mistake, it just won’t build, that way, if I screw up in any way, I can truly verify that no bugs would be pushed to the source code on GitHub (but this will annoy me, and you’ll see why).

Now, it did come with some really strange drawbacks that I am not used to. For example, if you set a variable and don’t use it whatsoever, the build will fail. That was something I was not prepared for because the HTTP client built-in provides two variables, errors, and response, and I only would really need one. Another interesting thing that is a drawback, in my opinion, is there’s no Object Oriented Programming built-in. This was something I am used to, as I use frameworks like Laravel, Symfony, and even custom ones I build myself. This does mean that all of the code is done procedurally, and even packages are made in a pretty weird way. Another interesting thing to note is that the syntax feels loads different from PHP. You actually HAVE to decide what type a variable is, and it can’t be mixed, it has to be a string, integer, float, boolean, or whatever else (with one exception, which I’ll cover in this).

First, I had to figure out what packages I needed, this can take a lot of time, because I had to search the already included packages for what I’m looking for. For example, I used “net/http” for the client. I also needed “fmt” for printing to the console, as well as “time” to be able to properly set a timeout for the client. The last package was “os” to be able to exit the script. Now, importing packages isn’t like using “use” statements in PHP, it’s using the keyword “import” followed by the packages you are using.

import (
"fmt"
"net/Http"
"time"
"os"
)

The next thing I needed was the function where the code starts, which is the main function. I then needed to use a really interesting operator “:=” in my code. This is what I was talking about in terms of the exception of choosing a type for your variables. I had to use it to create the HTTP Client, then to run a GET request (which is to example.com to make things simpler, and because I have never heard of it going offline).

client := http.Client{
Timeout: 5 * time.Second,
}
resp, err := client.Get("http://example.com")

Notice the “:=” in there? That means that it will get the variable type without me entering it. Also, variables don’t have a $ like you’d be used to in PHP. Their basically constants without any extra work. Another thing you may notice is I had to create two variables, “resp” and “err”. The “resp” is the response and “err” is literally just the error. I only needed one, so I figured I’d just use “err” as if theres no errors it will be “nil” (same as null in PHP).

if err != nil {
fmt.Println("Internet is down")
os.Exit(1)
} else {
fmt.Println("Internet is working")
os.Exit(0)
}

An interesting thing to note here is that “else” and “elseif” HAVE to be on the same line as the closing “}” before it. That will cause a build error if you don’t (GoLang probably already seems very strict at this point).

Now if you go to build it now, You can for sure bet there’s a build error, as if you noticed, I never used the “resp” variable. What am I supposed to do with it? I have to do the equivalent in GoLang as an “unset($var)” in PHP. And that, is one weird-looking line.

_ = resp

Yes. That is all. Just that before it runs the if statement, and BOOM, it builds. I spent 30 minutes on Google and StackOverflow, for that.

Now all I gotta do is go into my terminal, and build it:

go build -o up ./up.go

Now, let’s test it. I did add an extra line in the test that just says “Testing your internet connection” Before the client is ran. Just saying.

Here is what it shows if my internet is up.

Voila, a 4-hour project, that if I would’ve actually learned GoLang fully, probably would’ve took 10 minutes. You can see the code here, just read the README to see how to install it easily from the releases. I will be updating it with more features, but here you have it.

Hope you enjoyed my suffering with GoLang and it was worth it for you, now back to my Laravel project.

--

--