/goose

A lightweight web framework in Go

Primary LanguageGoMIT LicenseMIT

goose

goose is a lightweight web framework in Go.

Note: Currently, goose is still not ready to release. You should not use it for your project since the APIs may change a lot. Also, there are still many features for me to implement...

A hello world example
package main

import (
	"github.com/zhoudaxia233/goose"
)

func main() {
	g := goose.New()

	g.GET("/", func(ctx *goose.Context) {
		ctx.String("Hello World!")
	})

	g.Run(":8080")
}

Contents

Features

Dynamic Routing

An example
package main

import (
	"github.com/zhoudaxia233/goose"
)

func main() {
	g := goose.New()

	g.GET("/info/:name", func(ctx *goose.Context) {
		ctx.String("My name is %s", ctx.Param("name"))
	})

	g.Run(":8080")
}

Router Group

An example
package main

import (
	"github.com/zhoudaxia233/goose"
)

func main() {
	g := goose.New()

	v1 := g.Group("v1")
	{
		v1.GET("/", func(ctx *goose.Context) {
			ctx.String("Page V1!")
		})

		v1.GET("/hello", func(ctx *goose.Context) {
			ctx.String("Hello V1!")
		})

		// goose also supports nested router group
		v2 := v1.Group("v2")
		{
			v2.GET("/hello", func(ctx *goose.Context) {
				ctx.String("Hello V2!")
			})
		}
	}

	g.Run(":8080")
}

Middleware

An example
package main

import (
	"github.com/zhoudaxia233/goose"
)

func main() {
	g := goose.New()
	g.Use(func(ctx *goose.Context) {
		log.Println("here get executed before handling the request")
		ctx.Next()
		log.Println("here get executed after handling the request")
	})

	g.GET("/", func(ctx *goose.Context) {
		ctx.String("Hello World!")
	})

	v1 := g.Group("v1")
	v1.Use(func(ctx *goose.Context) {
		log.Println("before v1")
		ctx.Next()
		log.Println("after v1")
	})

	v1.GET("/hello", func(ctx *goose.Context) {
		ctx.String("Hello V1!")
	})

	g.Run(":8080")
}

Static Files

An example
package main

import (
	"github.com/zhoudaxia233/goose"
)

func main() {
	g := goose.New()

	g.Static("/assets", "examples/static")
	g.StaticFile("/favicon.ico", "examples/favicon.ico")

	g.Run(":8080")
}

Templates

An example
package main

import (
	"strconv"
	"strings"
	"time"

	"github.com/zhoudaxia233/goose"
)

func main() {
	g := goose.New()
	g.FuncMap(goose.X{
		"appendYear": func(s string) string {
			year := time.Now().Year()
			return strings.Join([]string{s, strconv.Itoa(year)}, " - ")
		},
	})
	g.Set("toUpper", strings.ToUpper)
	g.LoadHTMLGlob("testfiles/templates/*")

	g.GET("/", func(ctx *goose.Context) {
		ctx.HTML("hello.tmpl", goose.X{"name": "Goose"})
	})

	g.GET("/func", func(ctx *goose.Context) {
		ctx.HTML("funcmaps.tmpl", goose.X{"msg": "I love goose!"})
	})

	g.Run(":8080")
}

Acknowledgment

  1. I've got some useful design inspiration from Gin.
  2. The goose icon in the logo is made by monkik from Flaticon.