GoQuotes / goquotes.go

package main

// A quote server.
// This software is under GPL v3 license.
// Copyright 2012-2013 Cédric Bonhomme - http://cedricbonhomme.org/

import (

// Quotes are store in an array of Quote object.
type Quotes struct {
    Quotes []Quote `xml:"quote"`

// Defintion of the structure of a Quote object.
type Quote struct {
    Citation string `xml:"citation"`
    Author string `xml:"author"`
    Year string `xml:"year"`

// Read a file and return a string.
func read(file_name string) string {
    inputFile := file_name
    buf, err := ioutil.ReadFile(inputFile)
    if 1 != checkError(err) {
        return string(buf)
    return ""

// Check if an error occured
func checkError(err error) int {
    if err != nil {
        os.Stdout.WriteString("<!DOCTYPE html>\n<html>\n<head>\n<title>GoQuotes</title>\n")
        os.Stdout.WriteString("<meta charset=\"utf-8\" />\n")
        os.Stdout.WriteString("<link rel=\"stylesheet\" href=\"./style.css\" />\n</head>\n")
        os.Stdout.WriteString("Error " + err.Error())
        return 1
    return 0

// Return a random quote.
func getQuote(file_name string) {
    quotes_str := read(file_name)
    var quotes Quotes

    err := xml.Unmarshal([]byte(quotes_str), &quotes)
    if (1 != checkError(err)) {
        // Get the number of quotes
        nb_quotes := len(quotes.Quotes)
        quote := rand.Intn(nb_quotes)

        // Now use the Quote structure

// Print the quote (citation + author + date)
func printHTML(quote Quote) {
    os.Stdout.WriteString("<!DOCTYPE html>\n<html>\n<head>\n<title>GoQuotes</title>\n")
    os.Stdout.WriteString("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>\n")
    os.Stdout.WriteString("<link rel=\"stylesheet\" type=\"text/css\" href=\"./style.css\" />\n</head>\n")
    os.Stdout.WriteString("<blockquote>\"" + quote.Citation + "\"</blockquote>\n")
    if len(quote.Year) != 0 {
        os.Stdout.WriteString(" - " + quote.Year + "\n")
    } else {
    os.Stdout.WriteString("<!-- Generated with GoQuotes (https://bitbucket.org/cedricbonhomme/goquotes) -->\n")

// Main: you can specify here the location of the XML file of quotes.
func main() {
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.