LoginSignup
1

More than 5 years have passed since last update.

Goでtemplateをつかう

Posted at

Go: go version go1.6.3 darwin/amd64
Ref: https://golang.org/pkg/text/template/#pkg-examples

ご覧のとおりめちゃくちゃはしょっていますが脳内補完してください.

personal_data.tmpl
Name: {{ .Name }}
Mail: {{ .Mail }}
main.go
package main

import (
    "bytes"
    "fmt"
    "html/template"
)

func main() {
    type PersonalData struct {
        Name string
        Mail string
    }

    p := PersonalData{"おれです", "ore@example.com"}

    var b bytes.Buffer

    tpl, _ := template.ParseFiles("personal_data.tmpl")
    tpl.Execute(&b, p)

    fmt.Println(b.String())
    // Name: おれです
    // Mail: ore@example.com
}

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1