Simple Web App in Go Programming




Welcome to part 5 of the Go programming tutorial series, where we'll poke around a bit with the built-in web server with the Go Language.

To begin, we need to import net/http. You can learn more about net/http here.

package main

import ("fmt"
"net/http")

Next, we'll setup our main function:

func main() {
	http.HandleFunc("/", index_handler)
}

Here, we're using http.HandleFunc, which, as you might expect, handles functions that will coorespond to paths. In this case, we're going to use http.HandleFunc to, when the path of "/" is used (this would be your homepage), to run the index_handler function.

Next, still in our main function, we need to actually get the server to run. To do this:

	http.ListenAndServe(":8000", nil)

This is simple enough, listen on port 8000, and the nil is for server stuff, which needs something passed, but we're just going to pass nil for now, which is nothing.

Our full main function up to this point:

func main() {
	http.HandleFunc("/", index_handler)  // path, then what function to run
	http.ListenAndServe(":8000", nil) // listen on what port?   ... can serve on tls with ListenAndServeTLS ... need something in server args, we'll put nil for now
}

Okay, great, but we don't have an index_handler function, so let's make that next! To begin:

func index_handler(w http.ResponseWriter, r *http.Request) {
	
}

So what we put in here will run, but, first let's make sure we're clear what's happening in the parameters. Recall that variables and parameters require typing information, so, in this case, the w parameter is of the http.ResponseWriter type, and then r is what type? Since it's *http.Request, we're reading through the value of the request. Now, let's just put some text on the screen. We can do this with fmt.Fprintf:

	fmt.Fprintf(w, "Whoa, Go is neat!")

Fprintf formats by what is specified, and writes to the first parameter, w. In our case, this is going to write "Whoa, Go is neat!" to w, which is our http.ResponseWriter.

Full code up to this point:

package main

import ("fmt"
"net/http")

func index_handler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Whoa, Go is neat!")
}

func main() {
	http.HandleFunc("/", index_handler)
	http.ListenAndServe(":8000", nil) 
}

We can run this: go run goweb.go, and then open a browser, heading to 127.0.0.1:8000. If everything worked, you should see Whoa, Go is neat! on this page!

Let's say we wanted to add a new page, what would we do? It's actually pretty simple, we need to specify a new HandleFunc for the path, and the function itself. Let's try:

package main

import ("fmt"
"net/http")

func index_handler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Whoa, Go is neat!")
}

func about_handler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Expert web design by Harrison Kinsley")
}

func main() {
	http.HandleFunc("/", index_handler)
	http.HandleFunc("/about/", about_handler)
	http.ListenAndServe(":8000", nil) 
}

Running that, and then heading to http://127.0.0.1:8000/about/, we should see: Expert web design by Harrison Kinsley.

While we did cover all of the core concepts used in this example leading up to it, you might be a bit fuzzy on these types like http.ResponseWriter, what's that? How do these types of types come to exist? That's what we'll be covering in the next tutorial.

For the curious among us, check out:

func index_handler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Whoa, Go is neat!", r)
}

And, via terminal/console you can learn more about the ResponseWriter: godoc net/http ResponseWriter

The next tutorial:





  • Introduction to the Go Programming Language
  • Go Language Syntax
  • Go Language Types
  • Pointers in Go Programming
  • Simple Web App in Go Programming
  • Structs in the Go Programming Language
  • Methods in Go Programming
  • Pointer Receivers in Go Programming
  • More Web Dev in Go Language
  • Acessing the Internet in Go
  • Parsing XML with Go Programming
  • Looping in Go Programming
  • Continuing our Go Web application
  • Mapping in Golang
  • Mapping Golang sitemap data
  • Golang Web App HTML Templating
  • Applying templating to our Golang web app
  • Goroutines - Concurrency in Goprogramming
  • Synchronizing Goroutines - Concurrency in Golang
  • Defer - Golang
  • Panic and Recover in Go Programming
  • Go Channels - Concurrency in Go
  • Go Channels buffering, iteration, and synchronization
  • Adding Concurrency to speed up our Golang Web Application