Go Examples

Go programming examples to help you learn as I do

Go examples

This is a post to launch my new project. It’s a simple project that helps both myself and any budding Go programmers/people who just want to learn Go.

Disclaimer: I realise the code in the image above is not Go, ignore that

The new project, that is hosted here, will be not just a way for me to help others and myself to learn Go, but also it’ll act as an archive of code for when I need a reference or a more readable explanation than what I can find in the documentation. Currently (at the time of posting) I have an example of a basic http server and an example of how I setup and configure logging.

I will be uploading code to this repo as-and-when I learn it. I will also upload some already-known code in the meantime. This will probably be moved into the upcoming Wiki when it’s finished and out of development. Here is an example of the logging code that you can find on there:

Logging.go

package main

import (
	"fmt"
	"html"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"os"
)

// Create four kinds of loggers, each as a pointer to a log.Logger object
var (
	Trace   *log.Logger
	Info    *log.Logger
	Warning *log.Logger
	Error   *log.Logger
)

// Our function to initialise logging. It takes four devices as places to write errors
func InitLogging(
	traceHandle io.Writer,
	infoHandle io.Writer,
	warningHandle io.Writer,
	errorHandle io.Writer) {

	/*
	   Let's initialise logging -> we define a new log, give it a place to log to, a prefix for the message and pass some
	   flags with what else we want to be in the message. A flag defines the logging properties
	*/

	// Trace log
	Trace = log.New(traceHandle,
		"TRACE: ",
		log.Ldate|log.Ltime|log.Lshortfile)
	// Info log
	Info = log.New(infoHandle,
		"INFO: ",
		log.Ldate|log.Ltime|log.Lshortfile)
	// Warning log
	Warning = log.New(warningHandle,
		"WARNING: ",
		log.Ldate|log.Ltime|log.Lshortfile)
	// Error log
	Error = log.New(errorHandle,
		"ERROR: ",
		log.Ldate|log.Ltime|log.Lshortfile)
}

// We handle the user requesting everything except /five as just normal info
func handleHome(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello, %q\n", html.EscapeString(r.URL.Path))
	Info.Println("Request for ", html.EscapeString(r.URL.Path))
}

// We handle the user requesting /five as an error
func handleFive(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello, %q\n", html.EscapeString(r.URL.Path))
	Error.Println("They requested a 5")
}

// Entry point for the program.
func main() {
	// Call the Init function to get the logging setup and working
	InitLogging(ioutil.Discard, os.Stdout, os.Stdout, os.Stderr)

	// Set our application to listen on port 8080
	const port string = ":8080"

	// We configure two handlers, one to handle the erroneous /five and one to handle the rest
	http.HandleFunc("/", handleHome)
	http.HandleFunc("/five", handleFive)
	// Now we start the server, crashing as fatal when an error response is returned (quicker way of handling errors)
	log.Fatal(http.ListenAndServe(port, nil))
}

Well then, so far so good. Hopefully this will help the wannabe Go programmers out there. Enjoy!