Getting Started with Go
In this post we will be covering how to get started with using Go as our language of choice for creating a website. This will be a series that will take what I have learned so far to walk you through a basic website implementation. Keep in mind that this will be a fully functioning website. We will however only be using Go for the most part. I want to keep this focused on Go and therefore I made the decision to us Go Templates instead of some frontend framework as this will keep things simple. In the world of frontend development I feel there is a lot of confusion that on what framework to use, what css processor to use, should I do JavaSript or TypeScript. We don’t want any of that stuff here as we are wanting to use Go for majority of this.
In order to follow along with this series you will need to have a code editor. I use Vim and VS Code. VS Code is a great choice if you aren’t comfortable with terminal based editors.VS Code. Just follow along with the install wizard.
Basic Go Setup
We are going to get our development machine setup to use Go. If you already have Go installed and have created the necessary paths and everything else then please feel free to skip this section and go on to the next. If not please follow along. First up we need to visit the official site for Go. From this site we will do the following: 1. Click on the Download button in the center of the screen 2. Make sure you choose the version for your correct machine. In my case I am using an Intel based MacBook but I have also installed Go on a Windows machine and the installation steps are almost identical. Note: I am not using Homebrew as I want to make sure I get the latest version of the language and I want to keep this post simple for those using Windows as Homebrew doesn’t exist. If you are on mac please feel free to use Homebrew if you are comfortable with it. 3. After the download finishes go ahead and run the installation wizard following along with the steps until it finishes. This will be similar on both mac and Windows however Linux it might be easier to use the built in package manager for your system. 4. Verify that Go is installed by running the command in your terminal
$ go version
// should say something like the following
// => go version go1.19 darwin/amd64
Next we will run some commands that will be different depending on your OS. Please follow the steps based on your OS.
Windows
The command to run will be determined by what type of machine you have. Either 32 bit or 64 bit. If you aren’t sure right click on the Start menu and then click on System. You will see on the page that pops up something that says Windows 11 64 bit… or Windows 11 32 bit… I am not for sure what the message says just that within the first few lines you will see what arcitecture you have. Once you have figured that out. Open PowerShell and run the following command based on which architecture you have:
wget -c https://golang.org/dl/go1.15.6.windows-amd64.msi
//64bit
wget -c https://golang.org/dl/go1.15.6.windows-386.msi
//32bit
MacOS
Open up your terminal and run the following commands
mkdir -p $HOME/go/{bin,src,pkg}
This will create three directories inside of your go directory which was created when you installed Go through the installer.
Writing Our First Go App
Here we will write our first Go application. We will need to open up our terminal, if you don’t still have it open from running the command to check the version of Go. We are going to change into the directory that we created when we installed Go
cd go/src
mkdir sayHello
cd sayHello
go mod init
code .
There are two commands that might be new to you depending on what editor you use. The go mod init
command will initialize this directory as a Go module. We can now create our app using a more modularized approach similar to JavaScript or Python. The last command is new and if you aren’t familiar with it that’s ok. The command opens the current project directory with VS Code. If you get an error stating that code command not found open up VS Code and press command + shift + p on mac and ctrl + shift + p on Windows/Linux and in the box that pops up type Shell Command: Install ‘code’ command in PATH. You will need to restart VS Code. *Note: On mac you will need to close out of any terminal you have open as well as VS Code and then reopen them for the command to be activated.
Hello World
I know it’s a cliche these days to start your first app as a hello world project but I want to do things a bit differently in this section. We are going to write a Hello World app but then we are going to extend it and make it a Hello World website. So let’s get started.
- Creat a new file called
main.go
Next we will add our code
package main import ( "fmt" ) func main() { fmt.Println("Hello, World!") }
That’s it. That’s our first program. To verify that we have a working environment for Go and that there are no errors in our program let’s run it.
go run main.go
We should now see Hello, World! returned to us in our console. Pretty awesome right? Well that’s pretty cool but what if we wanted to pass in an argument to greet someone with our program. Well the way that it looks now we aren’t going to be able to do that. Let’s modify this code a bit.
package main
import (
"fmt"
)
func sayHello(name string) {
fmt.Println("Hello", name)
}
func main() {
sayHello("Jon")
}
In the above code we are modifying our main function to call the newly created sayHello function which takes in a name parameter of type string. Go is a statically typed langauge and therefore you must declare parameter types. In this case name is set to a string. Now inside this function we are just calling the fmt package which is Go’s format package for printing to the terminal similar to console.log()
or print()
in JavaScript and Python. Then inside of main we are calling our sayHello()
function and passing in a name in double quotes. For those of you coming from Python or JavaScript you can’t use single quotes in go to contain strings. Those are used for Runes. A data type we will talk about much later in this series. Right now all you need to know is you need to use double quotes ""
for strings in Go. Now let’s run this and see what we get.
$ go run main.go
Hello Jon
You should get back your greeting to whatever name you passed in as the parameter to your function. Pretty cool right? I know, I know. I hear you saying but Jon you said we were going to be using Go for web development this is only printing to the console. I can’t open this up in my browser. And I am glad you think so. Let’s do that now. Inside our main.go file we are going to replace the code once more and see how we can easily turn this app from a terminal app to a website. In our file modify the code so that it looks like this:
package main
import (
"fmt"
"net/http"
)
func helloHandler(w http.ResponseWriter, r *http.Response) {
fmt.Fprint(w, "<h1>Hello, World!</h1>")
}
func main() {
http.HandleFunc("/", helloHandler)
fmt.Println("Starting the server on :1414...")
http.ListenAndServe(":1414", nil)
}
The *http.Request
is a pointer. It is nothing more than an address in memory where some value is stored. Let’s test that this is running. Run our go run main.go
command and we should see a message printed to the screen that says Starting the server on :1414… let’s head over to the browser and look to see if our message shows up. Type in your browser localhost:1414 and press enter. You should see the Hello, World! message printed as an H1 heading.
Conclusion
That’s it for this first lesson. I hope that you enjoyed seeing the different ways you can write a Go program with nothing more than the standard library. No third party packages were used in the making of this website. I will be doing the second part shortly. Stay tuned.