Go Web Go

A Golang Wiki Page Tutorial Web Application
Alternatives To Go Web Go
Project NameStarsDownloadsRepos Using ThisPackages Using ThisMost Recent CommitTotal ReleasesLatest ReleaseOpen IssuesLicenseLanguage
2 days ago6otherHTML
现代 JavaScript 教程(The Modern JavaScript Tutorial)
Rails_apps_composer1,439815 years ago254September 23, 201853Ruby
A gem with recipes to create Rails application templates for Rails starter apps.
4 years ago1wtfplC++
486 lines of C++: old-school FPS in a weekend
8 days ago1otherPython
This is a repository for Community Mods made for the Borderlands series
Coffeescript Cookbook.github.io572
3 years ago10otherCSS
CoffeeScript Recipes, Examples and Tutorials
Orchid4811163 days ago22October 27, 201740gpl-3.0Kotlin
Build and deploy beautiful documentation sites that grow with you
6 years agoapache-2.0Java
Android Agera Example
4 days ago21otherC++
A ROS wrapper of the AprilTag 3 visual fiducial detector
Full Fledged Hledger253
6 months ago1bsd-3-clauseHaskell
Tutorial on Hledger setup with multi-year files, multi-source imports and a range of auto-generated reports
Oppia Android229
13 hours ago534apache-2.0Kotlin
A free, online & offline learning platform to make quality education accessible for all.
Alternatives To Go Web Go
Select To Compare

Alternative Project Comparisons


A Golang Wiki Page Tutorial Web Application


This Application takes a Go leaner through the process of building a web application using package net/http and html/template from Go standard library.

This is a stepping coding instructor to a tutorial article by golang.org. Each tagged commit is a separate teaching towards a very simple web app.

Covered in this tutorial:

  • Creating a data structure with load and save methods
  • Using the net/http package to build web applications
  • Using the html/template package to process HTML templates
  • Using the regexp package to validate user input
  • Using closures



  • A good place to learn about setting up git is here.
  • You can find documentation and download git here.


Assumed knowledge

  • Understanding of basic web technologies (HTTP, HTML)
  • Basic UNIX/DOS command-line knowledge

Commits / Tutorial Outline

You can check out any point of the tutorial using:

$ git checkout step-?

To see the changes made between any two lessons use the git diff command:

$ git diff step-?..step-?

Step-0 Bootstrapping

  • Create a gowiki.go file.
  • Add the Page struct.
  • Add the save and loadPage function.
  • Write the main function to test what we've written.

Step-1 Error Handling

  • Modify the loadPage to let it return an error if ReadFile encounters one.

You can compile and run the program like this:

$ go build gowiki.go
$ ./gowiki
This is a sample page.

Step-2 Serve Pages

  • Use net/http package to serve our wiki pages.
  • Add the viewHandler to handle URLs.
  • create some page data like test.txt

Let's compile, run our code and visit http://localhost:8080/view/test to see what we have by far.

Step-3 Editing Pages

  • Add the editHandler.

Now we have an editing page http://localhost:8080/edit/test.

As you might have noticed, we comment out the registration of saveHandler in main. We will come back on that later.

Step-4 Better HTML

  • Use html/template to keep the HTML in a separate file.
  • Keep hard-coded HTMLs in separate files.

Step-5 Refactor

In this step, we will not be adding any new functionality to our application. Instead, we are going to take a step back, refactor our codebase and fix a potential issue.

  • Move all templating code to its own function.
  • Handle non-existent pages in viewHandler.

Recompile the code, run and visit http://localhost:8080/view/APageThatDoesntExist

Step-6 Saving Pages

  • Implement the saveHandler and uncommenting the related line in main.

Step-7 More Error Handling

  • Handle errors in renderTemplate.
  • Handle errors in saveTemplate.

Step-8 Template Caching

  • Refactor to call ParseFiles once at program initialization, parsing all templates into a single *Template.
  • Create the global variable named templates, and initialize it with ParseFiles.
  • Modify the renderTemplate function to call the templates.ExecuteTemplate method with the name of the appropriate template.

Step-9 Validation

  • Use regexp package and create the global variable validPath to store our validation expression.
  • Add the function getTitle that uses the validPath expression to validate path and extract the page title.
  • Put a call to getTitle in each of the handlers.

Step-10 Using Function Literals and Closures

  • Re-write the function definition of each of the handlers to accept a title string.
  • Define the wrapper function that takes a function of the above type, and returns a function of type http.HandlerFunc.
  • Wrap the handler functions with makeHandler in main.
  • Remove the calls to getTitle from the handler functions, making them much simpler.

Try it out!

Now we have finished our little wiki page application. Recompile the code, and run the app:

$ go build gowiki.go
$ ./gowiki

Visiting http://localhost:8080/view/ANewPage should present you with the page edit form. You should then be able to enter some text, click 'Save', and be redirected to the newly created page.

More tasks

In the article there are some simple tasks left for readers:

  • Store templates in tmpl/ and page data in data/.
  • Add a handler to make the web root redirect to /view/FrontPage.
  • Spruce up the page templates by making them valid HTML and adding some CSS rules.
  • Implement inter-page linking by converting instances of [PageName] to <a href="/view/PageName">PageName</a>. (hint: you could use regexp.ReplaceAllFunc to do this)

And we will continue Go with these extra tasks.

Step-11 Refactor: File Structure

  • Store templates in tmpl/ and page data in data/.
  • Change file directory in save(), loadPage() and templates.
Popular Tutorials Projects
Popular Wiki Projects
Popular Learning Resources Categories
Related Searches

Get A Weekly Email With Trending Projects For These Categories
No Spam. Unsubscribe easily at any time.