Tuesday, March 14, 2017

Getting Started with Golang Part 3: Hello Web!

Typically when you start learning a programming language, you start by printing Hello World to a terminal. We will do that in this post. However, where Go really shines is in HTTP. So we will create a simple dynamically generated site which has a header saying Hello Web! with the request path below it.

First install Go:
pacman -S go
or
apt-get install golang
or
dnf install golang

Every Go file must start with a package name. If you want it to be runable you have to put it in the main package. So the first line should be: package main
Now we need to import the text formatting library "fmt". So we have:
import (
     "fmt"
)
As in C, the function called main is called when the program is started. In Go, it does not take any arguments or return anything. So the function declaration becomes:
func main() {
Now we want to call a function called Println from the fmt library. For acessing functions from libraries, we use dot accesses. Println takes a string and prints it out trailed by a newline. So our function call is:
fmt.Println("Hello World!")
Note that in Go you do not need semicolons at the end of lines (although they are valid syntax and you need them for most for loops).
Now you just add a close bracket.
Now lets format with golang: go fmt hello.go (assuming you saved it as hello.go)
And now we can run it: go run hello.go
Hello World!

Now here comes HTTP. This is surprisingly easy. First, just add "net/http" in your imports on a line after "fmt".
Now we need to define a function that will be called when there is an HTTP request. This takes two arguments: an HTTP response writer and an HTTP request. In Go, variable names come before the variable types. Thus, our function declaration becomes:
func handleRequest(w http.ResponseWriter, r http.Request) {
fmt in Go has a function called Fprintf, which does basically the same thing as it's C equivalent (substituting values into a string and writing it out to a stream). With Go we can use the ` character for multi-line strings. In Go, the := operator defines a variable and initializes it at the same time - with the variable being of the type it was initialized with. So to define our page variable:
page := `
<!DOCTYPE html>
<h1>Hello Web!</h1>
%s
`
Now, we need to use fmt.Fprintf to write this as an HTTP response:
fmt.Fprintf(w,page,r.URL.Path)
Close bracket the function and then we need to add code to main to start the server. Now we start the server. I am using port 8080 because it does not require root (normally) like 80 does. To start it:
http.ListenAndServe(":8080",handleRequest)
If you have an SSL certificate, you can use http.ListenAndServeTLS instead and put your cert file and key file paths after the ":8080" to use https (you must use your server domain then).

Now start your Go code and open a browser on the same machine and navigate to http://127.0.0.1:8080

You should now see your script working. Try adding an http path such as http://127.0.0.1:8080/hello.

No comments:

Post a Comment