Skip to content

Instantly share code, notes, and snippets.

@ykyuen
ykyuen / about_handler.go
Created April 8, 2019 18:00
handling-http-request-in-go-echo-framework-1-05
package handler
import (
"encoding/json"
"io/ioutil"
"net/http"
"github.com/labstack/echo"
"gitlab.com/ykyuen/golang-echo-template-example/model"
@ykyuen
ykyuen / example_response.go
Created April 8, 2019 17:59
handling-http-request-in-go-echo-framework-1-04
package model
type ExampleResponse struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Msg string `json:"msg"`
}
@ykyuen
ykyuen / main.go
Created April 8, 2019 17:57
handling-http-request-in-go-echo-framework-1-03
...
func main() {
// Echo instance
e := echo.New()
// Instantiate a template registry with an array of template set
// Ref: https://gist.github.com/rand99/808e6e9702c00ce64803d94abff65678
templates := make(map[string]*template.Template)
templates["home.html"] = template.Must(template.ParseFiles("view/home.html", "view/base.html"))
templates["about.html"] = template.Must(template.ParseFiles("view/about.html", "view/base.html"))
@ykyuen
ykyuen / get_full_name.go
Created April 8, 2019 17:55
handling-http-request-in-go-echo-framework-1-02
package api
import (
"fmt"
"net/http"
"github.com/labstack/echo"
"gitlab.com/ykyuen/golang-echo-template-example/model"
)
@ykyuen
ykyuen / example_request.go
Created April 8, 2019 17:54
handling-http-request-in-go-echo-framework-1-01
package model
type ExampleRequest struct {
FirstName string `json:"first_name" form:"first_name" query:"first_name"`
LastName string `json:"last_name" form:"last_name" query:"last_name"`
}
@ykyuen
ykyuen / about_handler.go
Created November 19, 2018 06:34
setup-nested-html-template-in-go-echo-web-framework-12
package handler
import (
"net/http"
"github.com/labstack/echo"
)
func AboutHandler(c echo.Context) error {
// Please note the the second parameter "about.html" is the template name and should
@ykyuen
ykyuen / about.html
Created November 19, 2018 06:33
setup-nested-html-template-in-go-echo-web-framework-11
{{define "title"}}
Boatswain Blog | {{index . "name"}}
{{end}}
{{define "body"}}
<h1>{{index . "msg"}}</h1>
<h2>This is the about page.</h2>
{{end}}
@ykyuen
ykyuen / base.html
Created November 19, 2018 06:32
setup-nested-html-template-in-go-echo-web-framework-10
{{define "base.html"}}
<!DOCTYPE html>
<html>
<head>
<title>{{template "title" .}}</title>
</head>
<body>
{{template "body" .}}
</body>
</html>
@ykyuen
ykyuen / main.go
Created November 19, 2018 06:29
setup-nested-html-template-in-go-echo-web-framework-09
package main
import (
"errors"
"html/template"
"io"
"github.com/labstack/echo"
"gitlab.com/ykyuen/golang-echo-template-example/handler"
@ykyuen
ykyuen / project.sh
Created November 19, 2018 06:23
setup-nested-html-template-in-go-echo-web-framework-08
golang-echo-template-example/
├── handler/ # folder of request handlers
│ ├── home_handler.go # handler for home page
│ └── about_handler.go # handler for about page
├── vendor/ # dependencies managed by dep
│ ├── github.com/*
│ └── golang.org/*
├── view/ # folder of html templates
│ ├── base.html # base layout template
│ ├── home.html # home page template