views

package module
v0.0.0-...-b881a79 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 28, 2014 License: MIT Imports: 5 Imported by: 1

README

GOLANG project template

This is my personal GOLANG project template. It is built with these goals in mind:

  • Everything should be local to the development folder as much as is possible without resorting to ugly hacks.

  • 1-step build or test without any special setup after any git clone on a newly minted machine except for installing the go compiler itself.

  • Do not rely on global GOPATH and yet still allows you to check your entire source folder in as if you would a normal go program inside one. This makes your repository plays well with other go coders who will surely be using the global GOPATH convention.

Getting Started

Run this one-liner to dump the repository content into current directory.

curl -L https://github.com/chakrit/go-scratch/archive/master.tar.gz | tar -xzv --strip 1

Do not forget to update the LICENSE file to match what you need. Or simply remove it.

Example for a full setup for a new project:

mkdir your-new-shiny-project    # Make a new shiny folder for your new project.
cd your-new-shiny-project       # Get into the folder first, of course.
git init                        # Or not, doesn't matter.

# Download the scratch template
curl -L https://github.com/chakrit/go-scratch/archive/master.tar.gz | tar -xzv --strip 1
# (optional) vim LICENSE        # Edit LICENSE file.

git add .
git commit -m "Initialize GOLANG project. (github.com/chakrit/go-scratch)"

Makefile

Everything is done through the Makefile for convenience. A wrapper script ./go is also provided that invokes go with GOPATH sets to the local .go folder.

Makefile tasks defaults to test. The all task is simply an alias for the build task. All common tasks you'd do with go is given the same name in the Makefile so go vet is, through the Makefile, make vet or via the wrapper script ./go vet.

Dependencies

Dependencies are handled implicitly and automatically as you run tasks that involve import paths thanks to the powerful go get command.

Specifically, make deps and make test-deps will download the dependencies into place and subsequent make test or make build will automaticaly compiles the downloaded dependencies as needed.

The initial main.go file provided with this project contains some dependencies as well as tests (and test dependencies) to demonstrate this.

Subpackages

This template supports multiple packages development inside a single folder out of the box. You are, however, required to list all the subpackage paths inside the Makefile PKG variable as automatically detecting them is tricky and error prone.

For example, if you have a context subpackage, edit the PKG line like so:

PKG := . ./context

... or if you have nothing in the root folder and only subpackages uno dos and tres:

PKG := ./uno ./dos ./tres

Example

Here's a sample output of what happens when you simply cloned this repository and issue make:

$ make
/Users/chakrit/Documents/go-scratch/go get -d -t .
/Users/chakrit/Documents/go-scratch/go test .
ok    _/Users/chakrit/Documents/go-scratch0.010s

Silent run

Uses make's -s flag to prevent make from echoing commands (useful for reducing clutter to standard output.)

$ make -s
ok      _/Users/chakrit/Documents/go-scratch    0.011s

LICENSE

WTFPL

TODO:

  • Package the curl installation into a script.

Documentation

Index

Examples

Constants

View Source
const RootTemplateName = "root"

Variables

This section is empty.

Functions

func EnableAutoReparse

func EnableAutoReparse()

func StopAutoReparse

func StopAutoReparse()

Types

type HtmlView

type HtmlView struct {
	// contains filtered or unexported fields
}

Represents a single html/template template. Encapsulate the template pathname from controller action code and allows further subviews based on this view.

Example
package main

import "io"
import "os"
import "net/http/httptest"
import . "github.com/zaiuz/views"
import "github.com/zaiuz/testutil"

var ParentView = NewHtmlView("./testviews/example-parent.html")

type ParentViewData struct {
	Title string
}

var ChildView = ParentView.Subview("./testviews/example-child.html")

type ChildViewData struct {
	*ParentViewData
	Content string
}

func main() {
	parent := &ParentViewData{Title: "ExampleRender Test Title"}
	child := &ChildViewData{
		ParentViewData: parent,
		Content:        "The quick brown fox jumps over the lazy dog.",
	}

	context := testutil.NewTestContext()

	ChildView.Render(context, child)

	recorder := context.ResponseWriter.(*httptest.ResponseRecorder)
	io.Copy(os.Stdout, recorder.Body)

}
Output:

<html>
<head>
<title>ExampleRender Test Title</title>
</head>
<body>
<h1>ExampleRender Test Title</h1>

<p>The quick brown fox jumps over the lazy dog.</p>

</body>
</html>

func (*HtmlView) Filenames

func (view *HtmlView) Filenames() []string

func (*HtmlView) Render

func (view *HtmlView) Render(c *z.Context, data interface{}) error

Renders the view to the response in the supplied Context with the given view data context.

func (*HtmlView) ReparseTemplate

func (view *HtmlView) ReparseTemplate() View

func (*HtmlView) Subview

func (view *HtmlView) Subview(filenames ...string) View

Creates a subview from the receiving view. Subview templates contains all templates defined in the parent view.

type TextView

type TextView struct {
	// contains filtered or unexported fields
}

func (*TextView) Render

func (view *TextView) Render(c *z.Context, data interface{}) error

func (*TextView) Subview

func (view *TextView) Subview(filenames ...string) View

type View

type View interface {
	Subview(filenames ...string) View
	Render(c *z.Context, data interface{}) error
}

func NewHtmlView

func NewHtmlView(filenames ...string) View

Creates a new html view from the specified template name which should be a html/template-compatible html template file.

func NewTextView

func NewTextView(filenames ...string) View

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL