Ver3.0 ๐ Go์ธ์ด ํ ํ๋ฆฟ๊ณผ html/template์ผ๋ก ๋์ ์น ํ์ด์ง ๋ง๋ค๊ธฐ ์์ ์ ๋ณต

๐ Go์ธ์ด ํ ํ๋ฆฟ๊ณผ html/template์ผ๋ก ๋์ ์น ํ์ด์ง ๋ง๋ค๊ธฐ ์์ ์ ๋ณต
์น๊ตฌ์ฒ๋ผ ์ฝ๊ฒ ๋ฐฐ์ฐ๋ Go ์น ๊ฐ๋ฐ์ ํต์ฌ ๊ธฐ์
๐ก ์ด ๊ธ์ ์ฝ์ผ๋ฉด ์ป์ ์ ์๋ ๊ฒ๋ค
Go ์ธ์ด๋ก ์น ๊ฐ๋ฐ์ ํ๋ค ๋ณด๋ฉด "์ด๋ป๊ฒ ํ๋ฉด HTML์ ๋์ ์ผ๋ก ์์ฑํ ์ ์์๊น?"๋ผ๋ ๊ณ ๋ฏผ์ ๋น ์ง๊ฒ ๋ผ. ์ ์ ์ธ HTML ํ์ผ๋ง์ผ๋ก๋ ์ฌ์ฉ์๋ง๋ค ๋ค๋ฅธ ์ ๋ณด๋ฅผ ๋ณด์ฌ์ฃผ๊ฑฐ๋, ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ๊ฐ์ ธ์จ ๋ด์ฉ์ ํ๋ฉด์ ํ์ํ๋ ๊ฒ ๋ถ๊ฐ๋ฅํ๊ฑฐ๋ . ๋ฐ๋ก ์ด๋ ํ์ํ ๊ฒ ํ
ํ๋ฆฟ ์์ง์ด์ผ! ๐ฏ
์ด ๊ธ์์๋ Go์ ๊ฐ๋ ฅํ ํ
ํ๋ฆฟ ์์คํ
์ธ html/template ํจํค์ง๋ฅผ ํ์ฉํด์ ๋์ ์น ํ์ด์ง๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ์ ์ฒ์๋ถํฐ ๋๊น์ง ์๋ ค์ค๊ฒ. ์ค๋ฌด์์ ๋ฐ๋ก ์ธ ์ ์๋ ์์ ์ ํจ๊ป ๋ง์ด์ผ!
๐ฏ ์ html/template์ ์จ์ผ ํ ๊น?
Go๋ก ์น ์๋ฒ๋ฅผ ๋ง๋ค ๋ ๊ฐ์ฅ ๋จผ์ ๋ง์ฃผํ๋ ๋ฌธ์ ๊ฐ ๋ฐ๋ก "์ด๋ป๊ฒ HTML์ ์์ฑํ ๊ฒ์ธ๊ฐ"์ผ. ๋ฌผ๋ก ๋ฌธ์์ด๋ก ์ง์ HTML์ ๋ง๋ค ์๋ ์์ด. ํ์ง๋ง ๊ทธ๊ฑด ์ ๋ง ๋นํจ์จ์ ์ด๊ณ ์ ์ง๋ณด์๊ฐ ์
๋ชฝ์ด ๋์ง. ๐ฑ
์๋ฅผ ๋ค์ด๋ณผ๊ฒ:
// ์ด๋ ๊ฒ ํ๋ฉด ์ ๋ผ! (๋์ ์์)
func handler(w http.ResponseWriter, r *http.Request) {
html := "<html><body><h1>" + title + "</h1><p>" + content + "</p></body></html>"
fmt.Fprintf(w, html)
}
์ ์ฝ๋์ ๋ฌธ์ ์ ์ด ๋ญ๊น? ๐ค
1๏ธโฃ ๋ณด์ ์ทจ์ฝ์ : XSS(Cross-Site Scripting) ๊ณต๊ฒฉ์ ๋
ธ์ถ๋ผ
2๏ธโฃ ๊ฐ๋
์ฑ ์ต์
: HTML ๊ตฌ์กฐ๋ฅผ ํ์
ํ๊ธฐ ์ด๋ ค์
3๏ธโฃ ์ ์ง๋ณด์ ์ง์ฅ: ๋์์ธ ๋ณ๊ฒฝํ ๋๋ง๋ค Go ์ฝ๋๋ฅผ ์์ ํด์ผ ํด
4๏ธโฃ ํ์
๋ถ๊ฐ: ํ๋ก ํธ์๋ ๊ฐ๋ฐ์์ ์์
๋ถ๋ฆฌ๊ฐ ์ ๋ผ
๋ฐ๋ก ์ด๋ฐ ๋ฌธ์ ๋ค์ ํด๊ฒฐํ๊ธฐ ์ํด html/template ํจํค์ง๊ฐ ์กด์ฌํ๋ ๊ฑฐ์ผ!
๐ก๏ธ ์๋ ์ด์ค์ผ์ดํ
XSS ๊ณต๊ฒฉ์ ์๋์ผ๋ก ๋ฐฉ์ดํด์ค. ์ฌ์ฉ์ ์ ๋ ฅ๊ฐ์ ์์ ํ๊ฒ ์ฒ๋ฆฌํ ์ ์์ด.
๐จ ์ฝ๋ ๋ถ๋ฆฌ
๋ก์ง๊ณผ ๋ทฐ๋ฅผ ์์ ํ ๋ถ๋ฆฌํ ์ ์์ด. ํ๋ก ํธ์๋ ๊ฐ๋ฐ์์ ํ์ ์ด ์ฌ์์ ธ.
โก ์ฑ๋ฅ ์ต์ ํ
ํ ํ๋ฆฟ์ ๋ฏธ๋ฆฌ ํ์ฑํด์ ์บ์ฑํ๋ฉด ๋น ๋ฅธ ๋ ๋๋ง์ด ๊ฐ๋ฅํด.
๐ง ๊ฐ๋ ฅํ ๊ธฐ๋ฅ
์กฐ๊ฑด๋ฌธ, ๋ฐ๋ณต๋ฌธ, ํ์ดํ๋ผ์ธ ๋ฑ ๋ค์ํ ํ ํ๋ฆฟ ๋ฌธ๋ฒ์ ์ ๊ณตํด.
๐ html/template ๊ธฐ๋ณธ ๊ฐ๋ ์ก๊ธฐ
๋ณธ๊ฒฉ์ ์ผ๋ก ์์ํ๊ธฐ ์ ์ ํต์ฌ ๊ฐ๋ ๋ถํฐ ์ ๋ฆฌํ๊ณ ๊ฐ์! Go์ ํ ํ๋ฆฟ ์์คํ ์ ํฌ๊ฒ ์ธ ๊ฐ์ง ์์๋ก ๊ตฌ์ฑ๋ผ ์์ด.
ํ ํ๋ฆฟ ์ ์ (Template Definition)
HTML ํ์ผ์ Go ํ
ํ๋ฆฟ ๋ฌธ๋ฒ์ ์์ด์ ์์ฑํด. ํ์ผ ํ์ฅ์๋ ๋ณดํต .html ๋๋ .tmpl์ ์ฌ์ฉํด. ํ
ํ๋ฆฟ ์์์๋ ์ด์ค ์ค๊ดํธ {{ }}๋ฅผ ์ฌ์ฉํด์ ๋์ ์ธ ๋ถ๋ถ์ ํ์ํ์ง.
ํ ํ๋ฆฟ ํ์ฑ (Template Parsing)
Go ์ฝ๋์์ template.ParseFiles() ๋๋ template.ParseGlob() ํจ์๋ฅผ ์ฌ์ฉํด์ ํ
ํ๋ฆฟ ํ์ผ์ ์ฝ์ด๋ค์ฌ. ์ด ๊ณผ์ ์์ ํ
ํ๋ฆฟ ๋ฌธ๋ฒ์ ๋ถ์ํ๊ณ ๋ด๋ถ ๊ตฌ์กฐ๋ก ๋ณํํด.
ํ ํ๋ฆฟ ์คํ (Template Execution)
Execute() ๋๋ ExecuteTemplate() ๋ฉ์๋๋ก ๋ฐ์ดํฐ๋ฅผ ํ
ํ๋ฆฟ์ ์ฃผ์
ํ๊ณ ์ต์ข
HTML์ ์์ฑํด. ์ด๋ ๋ฐ์ดํฐ๋ ๊ตฌ์กฐ์ฒด, ๋งต, ์ฌ๋ผ์ด์ค ๋ฑ ๋ค์ํ ํํ๊ฐ ๋ ์ ์์ด.
๐ก Pro Tip ์ค๋ฌด์์๋ ์๋ฒ ์์ ์ ํ ํ๋ฆฟ์ ํ ๋ฒ๋ง ํ์ฑํด์ ๋ฉ๋ชจ๋ฆฌ์ ์บ์ฑํด๋๊ณ , ์์ฒญ์ด ์ฌ ๋๋ง๋ค Execute๋ง ๋ฐ๋ณตํ๋ ๋ฐฉ์์ ์ฌ์ฉํด. ์ด๋ ๊ฒ ํ๋ฉด ์ฑ๋ฅ์ด ํจ์ฌ ์ข์์ ธ!
๐ ์ฒซ ๋ฒ์งธ ํ ํ๋ฆฟ ๋ง๋ค์ด๋ณด๊ธฐ
์ด๋ก ์ ์ด ์ ๋๋ฉด ์ถฉ๋ถํด. ์ด์ ์ง์ ์ฝ๋๋ฅผ ์์ฑํ๋ฉด์ ๋ฐฐ์๋ณด์! ๊ฐ์ฅ ๊ฐ๋จํ ์์ ๋ถํฐ ์์ํ ๊ฒ. ๐
Step 1: ํ๋ก์ ํธ ๊ตฌ์กฐ ๋ง๋ค๊ธฐ
๋จผ์ ํ๋ก์ ํธ ํด๋๋ฅผ ๋ง๋ค๊ณ ๋ค์๊ณผ ๊ฐ์ ๊ตฌ์กฐ๋ก ํ์ผ์ ๋ฐฐ์นํด:
myproject/
โโโ main.go
โโโ templates/
โโโ index.html
Step 2: ํ ํ๋ฆฟ ํ์ผ ์์ฑํ๊ธฐ
templates/index.html ํ์ผ์ ๋ง๋ค๊ณ ๋ค์ ๋ด์ฉ์ ์
๋ ฅํด:
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{.Title}}</title>
<style>
body { font-family: Arial, sans-serif; max-width: 800px; margin: 50px auto; padding: 20px; }
h1 { color: #667eea; }
.content { background: #f8f9fa; padding: 20px; border-radius: 8px; }
</style>
</head>
<body>
<h1>{{.Title}}</h1>
<div class="content">
<p>{{.Message}}</p>
<p>ํ์ฌ ์๊ฐ: {{.Time}}</p>
</div>
</body>
</html>
์ฌ๊ธฐ์ {{.Title}}, {{.Message}}, {{.Time}}์ด ๋ฐ๋ก ๋์ ์ผ๋ก ์ฑ์์ง ๋ถ๋ถ์ด์ผ. ์ (.)์ ํ์ฌ ์ปจํ
์คํธ๋ฅผ ์๋ฏธํ๊ณ , ๊ทธ ๋ค์ ์ด๋ฆ์ ๋ฐ์ดํฐ ํ๋๋ฅผ ๊ฐ๋ฆฌ์ผ.
Step 3: Go ์๋ฒ ์ฝ๋ ์์ฑํ๊ธฐ
์ด์ main.go ํ์ผ์ ์์ฑํด๋ณด์:
package main
import (
"html/template"
"log"
"net/http"
"time"
)
// ํ
ํ๋ฆฟ์ ์ ๋ฌํ ๋ฐ์ดํฐ ๊ตฌ์กฐ์ฒด
type PageData struct {
Title string
Message string
Time string
}
func main() {
// ํ
ํ๋ฆฟ ํ์ฑ (์๋ฒ ์์ ์ ํ ๋ฒ๋ง)
tmpl, err := template.ParseFiles("templates/index.html")
if err != nil {
log.Fatal("ํ
ํ๋ฆฟ ํ์ฑ ์๋ฌ:", err)
}
// ํธ๋ค๋ฌ ํจ์ ๋ฑ๋ก
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// ํ
ํ๋ฆฟ์ ์ ๋ฌํ ๋ฐ์ดํฐ ์ค๋น
data := PageData{
Title: "Go ํ
ํ๋ฆฟ ์ฒซ ๊ฑธ์! ๐",
Message: "html/template์ ์ฌ์ฉํ ๋์ ์น ํ์ด์ง์
๋๋ค.",
Time: time.Now().Format("2006-01-02 15:04:05"),
}
// ํ
ํ๋ฆฟ ์คํ ๋ฐ ์๋ต ์ ์ก
err := tmpl.Execute(w, data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
// ์๋ฒ ์์
log.Println("์๋ฒ ์์: http://localhost:8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
Step 4: ์คํํ๊ณ ํ์ธํ๊ธฐ
ํฐ๋ฏธ๋์์ ๋ค์ ๋ช ๋ น์ด๋ฅผ ์คํํด:
go run main.go
๋ธ๋ผ์ฐ์ ์์ http://localhost:8080์ ์ ์ํ๋ฉด ๋์ ์ผ๋ก ์์ฑ๋ ํ์ด์ง๋ฅผ ๋ณผ ์ ์์ด! ์๋ก๊ณ ์นจํ ๋๋ง๋ค ์๊ฐ์ด ์
๋ฐ์ดํธ๋๋ ๊ฑธ ํ์ธํ ์ ์์ ๊ฑฐ์ผ. โฐ
๐ ํ์ต ํฌ์ธํธ
1. template.ParseFiles()๋ก ํ
ํ๋ฆฟ ํ์ผ์ ์ฝ์ด๋ค์์ด
2. ๊ตฌ์กฐ์ฒด๋ฅผ ์ฌ์ฉํด์ ๋ฐ์ดํฐ๋ฅผ ๊ตฌ์กฐํํ์ด
3. tmpl.Execute()๋ก ๋ฐ์ดํฐ๋ฅผ ํ
ํ๋ฆฟ์ ์ฃผ์
ํ๊ณ HTTP ์๋ต์ผ๋ก ์ ์กํ์ด
4. ํ
ํ๋ฆฟ ์์์ {{.ํ๋๋ช
}} ๋ฌธ๋ฒ์ผ๋ก ๋ฐ์ดํฐ์ ์ ๊ทผํ์ด
๐ง ํ ํ๋ฆฟ ๋ฌธ๋ฒ ์์ ์ ๋ณต
์ด์ ๋ณธ๊ฒฉ์ ์ผ๋ก ํ ํ๋ฆฟ ๋ฌธ๋ฒ์ ๋ฐฐ์๋ณด์! html/template์ ์ ๋ง ๊ฐ๋ ฅํ ๊ธฐ๋ฅ๋ค์ ์ ๊ณตํด. ํ๋์ฉ ์ดํด๋ณผ๊ฒ. ๐ช
1๏ธโฃ ๋ณ์ ์ถ๋ ฅํ๊ธฐ
๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ๊ธฐ๋ฅ์ด์ผ. ์ (.)์ผ๋ก ์์ํ๋ ํํ์์ผ๋ก ๋ฐ์ดํฐ์ ์ ๊ทผํด.
{{.}} - ํ์ฌ ์ปจํ
์คํธ ์ ์ฒด
{{.Name}} - Name ํ๋
{{.User.Email}} - ์ค์ฒฉ๋ ๊ตฌ์กฐ์ฒด์ Email ํ๋
{{index .Items 0}} - ์ฌ๋ผ์ด์ค์ ์ฒซ ๋ฒ์งธ ์์
2๏ธโฃ ์กฐ๊ฑด๋ฌธ (if/else)
์กฐ๊ฑด์ ๋ฐ๋ผ ๋ค๋ฅธ ๋ด์ฉ์ ๋ณด์ฌ์ค ์ ์์ด. Go์ if๋ฌธ๊ณผ ๋น์ทํ์ง๋ง ๋ฌธ๋ฒ์ด ์กฐ๊ธ ๋ฌ๋ผ.
{{if .IsLoggedIn}}
<p>ํ์ํฉ๋๋ค, {{.Username}}๋!</p>
{{else}}
<p>๋ก๊ทธ์ธ์ด ํ์ํฉ๋๋ค.</p>
{{end}}
{{if gt .Score 80}}
<p>ํฉ๊ฒฉ์
๋๋ค! ๐</p>
{{else if gt .Score 60}}
<p>๋ณดํต์
๋๋ค.</p>
{{else}}
<p>๋ถํฉ๊ฒฉ์
๋๋ค. ๐ข</p>
{{end}}
๐ก ๋น๊ต ์ฐ์ฐ์ ์์๋๊ธฐ
โข eq: ๊ฐ์ (equal)
โข ne: ๋ค๋ฆ (not equal)
โข lt: ์์ (less than)
โข le: ์๊ฑฐ๋ ๊ฐ์ (less than or equal)
โข gt: ํผ (greater than)
โข ge: ํฌ๊ฑฐ๋ ๊ฐ์ (greater than or equal)
3๏ธโฃ ๋ฐ๋ณต๋ฌธ (range)
์ฌ๋ผ์ด์ค๋ ๋งต์ ์ํํ ๋ ์ฌ์ฉํด. ์ด๊ฑด ์ ๋ง ์์ฃผ ์ฐ์ด๋ ๊ธฐ๋ฅ์ด์ผ!
{{range .Items}}
<li>{{.Name}} - {{.Price}}์</li>
{{end}}
{{range $index, $item := .Products}}
<p>{{$index}}๋ฒ: {{$item.Name}}</p>
{{end}}
{{range .Users}}
<div>{{.Name}} ({{.Email}})</div>
{{else}}
<p>์ฌ์ฉ์๊ฐ ์์ต๋๋ค.</p>
{{end}}
range์ else ๋ธ๋ก์ ์ฌ๋ผ์ด์ค๊ฐ ๋น์ด์์ ๋ ์คํ๋ผ. ์ด๊ฑฐ ์ง์ง ์ ์ฉํด! ๐
4๏ธโฃ ๋ณ์ ์ ์ธ (with)
with๋ฅผ ์ฌ์ฉํ๋ฉด ๊ธด ๊ฒฝ๋ก๋ฅผ ์งง๊ฒ ์ค์ผ ์ ์์ด. ์ฝ๋๊ฐ ํจ์ฌ ๊น๋ํด์ ธ.
{{with .User}}
<h2>{{.Name}}</h2>
<p>์ด๋ฉ์ผ: {{.Email}}</p>
<p>๋์ด: {{.Age}}์ธ</p>
{{end}}
{{with .User.Profile}}
<img src="{{.Avatar}}" alt="ํ๋กํ">
<p>{{.Bio}}</p>
{{else}}
<p>ํ๋กํ ์ ๋ณด๊ฐ ์์ต๋๋ค.</p>
{{end}}
5๏ธโฃ ํ์ดํ๋ผ์ธ (Pipeline)
์ ๋์ค ํ์ดํ์ฒ๋ผ ํจ์๋ฅผ ์ฐ๊ฒฐํด์ ์ฌ์ฉํ ์ ์์ด. ์ ๋ง ๊ฐ๋ ฅํ ๊ธฐ๋ฅ์ด์ผ! ๐ฅ
{{.Name | printf "์๋
ํ์ธ์, %s๋"}}
{{.Content | html}}
{{.Price | printf "%.2f์"}}
{{.Text | len}} - ๋ฌธ์์ด ๊ธธ์ด ์ถ๋ ฅ
6๏ธโฃ ์ฃผ์
ํ ํ๋ฆฟ ์์์ ์ฃผ์์ ๋ฌ ์ ์์ด. ์ต์ข HTML์๋ ํฌํจ๋์ง ์์.
{{/* ์ด๊ฒ์ ํ
ํ๋ฆฟ ์ฃผ์์
๋๋ค */}}
{{/*
์ฌ๋ฌ ์ค ์ฃผ์๋
๊ฐ๋ฅํฉ๋๋ค
*/}}
๐ ์ค์ ์์ : ์ํ ๋ชฉ๋ก ํ์ด์ง ์ด์ ๋ฐฐ์ด ๋ฌธ๋ฒ๋ค์ ์กฐํฉํด์ ์ค์ ์ํ ๋ชฉ๋ก ํ์ด์ง๋ฅผ ๋ง๋ค์ด๋ณด์!
// Go ์ฝ๋
type Product struct {
ID int
Name string
Price int
InStock bool
Description string
}
type PageData struct {
Title string
Products []Product
Total int
}
func handler(w http.ResponseWriter, r *http.Request) {
data := PageData{
Title: "์ํ ๋ชฉ๋ก",
Products: []Product{
{1, "๋
ธํธ๋ถ", 1500000, true, "๊ณ ์ฑ๋ฅ ๋
ธํธ๋ถ"},
{2, "๋ง์ฐ์ค", 30000, true, "๋ฌด์ ๋ง์ฐ์ค"},
{3, "ํค๋ณด๋", 80000, false, "๊ธฐ๊ณ์ ํค๋ณด๋"},
},
Total: 3,
}
tmpl.Execute(w, data)
}
<!-- ํ
ํ๋ฆฟ ํ์ผ -->
<h1>{{.Title}}</h1>
<p>์ด {{.Total}}๊ฐ์ ์ํ์ด ์์ต๋๋ค.</p>
{{if .Products}}
<div class="product-list">
{{range $index, $product := .Products}}
<div class="product-card">
<h3>{{$product.Name}}</h3>
<p>๊ฐ๊ฒฉ: {{$product.Price | printf "%d์"}}</p>
<p>{{$product.Description}}</p>
{{if $product.InStock}}
<span class="badge-success">์ฌ๊ณ ์์ โ
</span>
{{else}}
<span class="badge-danger">ํ์ โ</span>
{{end}}
</div>
{{end}}
</div>
{{else}}
<p>๋ฑ๋ก๋ ์ํ์ด ์์ต๋๋ค.</p>
{{end}}
๐จ ํ ํ๋ฆฟ ์์๊ณผ ์ฌ์ฌ์ฉ
์ค์ ์น ์ฌ์ดํธ๋ฅผ ๋ง๋ค๋ค ๋ณด๋ฉด ์ฌ๋ฌ ํ์ด์ง์์ ๊ณตํต์ผ๋ก ์ฌ์ฉํ๋ ๋ถ๋ถ์ด ๋ง์. ํค๋, ํธํฐ, ๋ค๋น๊ฒ์ด์
๊ฐ์ ๊ฑฐ ๋ง์ด์ผ. ๋งค๋ฒ ๋ณต์ฌ-๋ถ์ฌ๋ฃ๊ธฐ ํ๋ฉด ์ ์ง๋ณด์๊ฐ ์ ๋ง ํ๋ค์ด์ง์ง. ๐
Go ํ
ํ๋ฆฟ์ ํ
ํ๋ฆฟ ์์๊ณผ ๋ถ๋ถ ํ
ํ๋ฆฟ(Partial) ๊ธฐ๋ฅ์ ์ ๊ณตํด์ ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํด์ค!
๐ฆ ํ ํ๋ฆฟ ์ ์ํ๊ธฐ (define)
define ์ก์
์ ์ฌ์ฉํ๋ฉด ์ฌ์ฌ์ฉ ๊ฐ๋ฅํ ํ
ํ๋ฆฟ ๋ธ๋ก์ ๋ง๋ค ์ ์์ด.
<!-- templates/layout.html -->
{{define "layout"}}
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>{{template "title" .}}</title>
<link rel="stylesheet" href="/static/style.css">
</head>
<body>
{{template "header" .}}
<main>
{{template "content" .}}
</main>
{{template "footer" .}}
</body>
</html>
{{end}}
{{define "header"}}
<header>
<nav>
<a href="/">ํ</a>
<a href="/products">์ํ</a>
<a href="/about">์๊ฐ</a>
</nav>
</header>
{{end}}
{{define "footer"}}
<footer>
<p>ยฉ 2024 My Website. All rights reserved.</p>
</footer>
{{end}}
์ด์ ๊ฐ๋ณ ํ์ด์ง์์๋ content์ title ๋ถ๋ถ๋ง ์ ์ํ๋ฉด ๋ผ!
<!-- templates/home.html -->
{{define "title"}}ํ - My Website{{end}}
{{define "content"}}
<h1>ํ์ํฉ๋๋ค! ๐</h1>
<p>์ด๊ฒ์ ํ ํ์ด์ง์
๋๋ค.</p>
{{end}}
<!-- templates/products.html -->
{{define "title"}}์ํ ๋ชฉ๋ก - My Website{{end}}
{{define "content"}}
<h1>์ํ ๋ชฉ๋ก ๐ฆ</h1>
{{range .Products}}
<div class="product">
<h3>{{.Name}}</h3>
<p>{{.Price}}์</p>
</div>
{{end}}
{{end}}
Go ์ฝ๋์์๋ ์ด๋ ๊ฒ ์ฌ์ฉํด:
package main
import (
"html/template"
"log"
"net/http"
)
var templates *template.Template
func init() {
// ๋ชจ๋ ํ
ํ๋ฆฟ ํ์ผ์ ํ ๋ฒ์ ํ์ฑ
templates = template.Must(template.ParseGlob("templates/*.html"))
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
// layout ํ
ํ๋ฆฟ์ ์คํํ๋ฉด home.html์ content๊ฐ ์ฝ์
๋จ
err := templates.ExecuteTemplate(w, "layout", nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func productsHandler(w http.ResponseWriter, r *http.Request) {
data := struct {
Products []Product
}{
Products: getProducts(), // ์ํ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
}
err := templates.ExecuteTemplate(w, "layout", data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func main() {
http.HandleFunc("/", homeHandler)
http.HandleFunc("/products", productsHandler)
log.Println("์๋ฒ ์์: http://localhost:8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
๐ก template.Must()์ ์ญํ
template.Must()๋ ํ
ํ๋ฆฟ ํ์ฑ ์ค ์๋ฌ๊ฐ ๋ฐ์ํ๋ฉด ํจ๋์ ์ผ์ผ์ผ์ ํ๋ก๊ทธ๋จ์ ์ฆ์ ์ข
๋ฃ์์ผ. ์๋ฒ ์์ ๋จ๊ณ์์ ํ
ํ๋ฆฟ ๋ฌธ๋ฒ ์ค๋ฅ๋ฅผ ๋ฐ๋ก ์ก์ ์ ์์ด์ ์ ์ฉํด!
๐ ํ ํ๋ฆฟ ๋ธ๋ก (block)
block์ define๊ณผ ๋น์ทํ์ง๋ง, ๊ธฐ๋ณธ๊ฐ์ ์ ๊ณตํ ์ ์์ด. ์์ ํ
ํ๋ฆฟ์์ ์ฌ์ ์ํ์ง ์์ผ๋ฉด ๊ธฐ๋ณธ๊ฐ์ด ์ฌ์ฉ๋ผ.
{{define "layout"}}
<!DOCTYPE html>
<html>
<head>
<title>{{block "title" .}}๊ธฐ๋ณธ ์ ๋ชฉ{{end}}</title>
</head>
<body>
{{block "content" .}}
<p>๊ธฐ๋ณธ ์ปจํ
์ธ ์
๋๋ค.</p>
{{end}}
</body>
</html>
{{end}}
์ด๋ ๊ฒ ํ๋ฉด ์์ ํ
ํ๋ฆฟ์์ title์ด๋ content๋ฅผ ์ ์ํ์ง ์์๋ ์๋ฌ๊ฐ ๋์ง ์์. ๊ธฐ๋ณธ๊ฐ์ด ํ์๋์ง!
๐ก๏ธ ๋ณด์: XSS ๋ฐฉ์ด์ ์ด์ค์ผ์ดํ
์น ๊ฐ๋ฐ์์ ๋ณด์์ ์ ๋ง ์ค์ํด! ํนํ ์ฌ์ฉ์ ์ ๋ ฅ์ ํ๋ฉด์ ํ์ํ ๋๋ ๋์ฑ ์กฐ์ฌํด์ผ ํด. html/template ํจํค์ง๋ ์๋์ผ๋ก XSS ๊ณต๊ฒฉ์ ๋ฐฉ์ดํด์ฃผ๋๋ฐ, ์ด๋ป๊ฒ ์๋ํ๋์ง ์์๋ณด์. ๐
โ ๏ธ XSS ๊ณต๊ฒฉ์ด๋?
XSS(Cross-Site Scripting)๋ ์ ์์ ์ธ ์คํฌ๋ฆฝํธ๋ฅผ ์น ํ์ด์ง์ ์ฝ์ ํ๋ ๊ณต๊ฒฉ์ด์ผ. ์๋ฅผ ๋ค์ด:
// ์ฌ์ฉ์๊ฐ ์
๋ ฅํ ๊ฐ
userInput := "<script>alert('ํดํน!')</script>"
// text/template์ ์ฌ์ฉํ๋ฉด (์ํ!)
// ์คํฌ๋ฆฝํธ๊ฐ ๊ทธ๋๋ก ์คํ๋จ
// html/template์ ์ฌ์ฉํ๋ฉด (์์ !)
// <script>๊ฐ <script>๋ก ์๋ ๋ณํ๋จ
โ ์๋ ์ด์ค์ผ์ดํ
html/template์ ์ปจํ ์คํธ๋ฅผ ์ธ์ํด์ ์๋์ผ๋ก ์ ์ ํ ์ด์ค์ผ์ดํ์ ์ ์ฉํด:
| ์ปจํ ์คํธ | ์ ๋ ฅ๊ฐ | ์ถ๋ ฅ ๊ฒฐ๊ณผ |
|---|---|---|
| HTML ๋ณธ๋ฌธ | <script>alert('XSS')</script> | <script>alert('XSS')</script> |
| HTML ์์ฑ | " onclick="alert('XSS') | " onclick="alert('XSS') |
| JavaScript | '; alert('XSS'); ' | \x27; alert(\x27XSS\x27); \x27 |
| URL | javascript:alert('XSS') | #ZgotmplZ (์์ ํ์ง ์์ URL ์ฐจ๋จ) |
๐ ์ค์ ์์ ๋ก ํ์ธํ๊ธฐ
package main
import (
"html/template"
"net/http"
)
type CommentData struct {
Username string
Comment string
}
func main() {
tmpl := template.Must(template.New("comment").Parse(`
<div class="comment">
<strong>{{.Username}}</strong>
<p>{{.Comment}}</p>
</div>
`))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// ์
์์ ์ธ ์
๋ ฅ ์๋ฎฌ๋ ์ด์
data := CommentData{
Username: "ํด์ปค",
Comment: "<script>alert('XSS ๊ณต๊ฒฉ!')</script>์๋
ํ์ธ์",
}
tmpl.Execute(w, data)
// ๊ฒฐ๊ณผ: ์คํฌ๋ฆฝํธ๊ฐ ์คํ๋์ง ์๊ณ ํ
์คํธ๋ก ํ์๋จ!
})
http.ListenAndServe(":8080", nil)
}
๐ ์๋์ ์ผ๋ก HTML ํ์ฉํ๊ธฐ
๋๋ก๋ ์ ๋ขฐํ ์ ์๋ HTML์ ๊ทธ๋๋ก ์ถ๋ ฅํด์ผ ํ ๋๊ฐ ์์ด. ์๋ฅผ ๋ค์ด ๊ด๋ฆฌ์๊ฐ ์์ฑํ ๊ณต์ง์ฌํญ ๊ฐ์ ๊ฑฐ. ์ด๋ด ๋๋ template.HTML ํ์
์ ์ฌ์ฉํด:
package main
import (
"html/template"
"net/http"
)
type PageData struct {
SafeHTML template.HTML // ์ด์ค์ผ์ดํ ์ ํจ
UnsafeHTML string // ์ด์ค์ผ์ดํ ํจ
}
func main() {
tmpl := template.Must(template.ParseFiles("page.html"))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
data := PageData{
SafeHTML: template.HTML("<strong>๊ตต์ ๊ธ์จ</strong>"),
UnsafeHTML: "<strong>๊ตต์ ๊ธ์จ</strong>",
}
tmpl.Execute(w, data)
})
http.ListenAndServe(":8080", nil)
}
<!-- page.html -->
<div>
<p>SafeHTML: {{.SafeHTML}}</p>
<!-- ์ถ๋ ฅ: SafeHTML: **๊ตต์ ๊ธ์จ** (์ค์ ๋ก ๊ตต๊ฒ ํ์๋จ) -->
<p>UnsafeHTML: {{.UnsafeHTML}}</p>
<!-- ์ถ๋ ฅ: UnsafeHTML: <strong>๊ตต์ ๊ธ์จ</strong> (ํ๊ทธ๊ฐ ํ
์คํธ๋ก ํ์๋จ) -->
</div>
โ ๏ธ ์ฃผ์์ฌํญ
template.HTML, template.JS, template.URL ๋ฑ์ ํ์
์ ์ด์ค์ผ์ดํ์ ์ฐํํ๋ฏ๋ก ์ ๋๋ก ์ฌ์ฉ์ ์
๋ ฅ์ ์ง์ ์ฌ์ฉํ๋ฉด ์ ๋ผ! ์ ๋ขฐํ ์ ์๋ ์์ค์์๋ง ์ฌ์ฉํด์ผ ํด.
๐ฏ ์์ ํ ํ์ ๋ค
html/template ํจํค์ง๋ ๋ค์ํ ์์ ํ ํ์ ์ ์ ๊ณตํด:
template.HTML // HTML ์ปจํ
์ธ
template.HTMLAttr // HTML ์์ฑ
template.CSS // CSS ์คํ์ผ
template.JS // JavaScript ์ฝ๋
template.JSStr // JavaScript ๋ฌธ์์ด
template.URL // URL
template.Srcset // img srcset ์์ฑ
๊ฐ ํ์ ์ ํด๋น ์ปจํ ์คํธ์์ ์์ ํ๊ฒ ์ฌ์ฉ๋ ์ ์๋๋ก ์ค๊ณ๋์์ด. ํ์ง๋ง ๋ฐ๋ณตํ์ง๋ง, ์ฌ์ฉ์ ์ ๋ ฅ์๋ ์ ๋ ์ฌ์ฉํ์ง ๋ง!
โก ์ฑ๋ฅ ์ต์ ํ ์ ๋ต
ํ ํ๋ฆฟ ๋ ๋๋ง์ ์๊ฐ๋ณด๋ค ๋น์ฉ์ด ๋ง์ด ๋๋ ์์ ์ด์ผ. ํนํ ํธ๋ํฝ์ด ๋ง์ ์๋น์ค์์๋ ์ฑ๋ฅ ์ต์ ํ๊ฐ ํ์์ ์ด์ง. ์ค๋ฌด์์ ๋ฐ๋ก ์ธ ์ ์๋ ์ต์ ํ ๊ธฐ๋ฒ๋ค์ ์๋ ค์ค๊ฒ! ๐
1๏ธโฃ ํ ํ๋ฆฟ ์บ์ฑ
๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ด๋ฉด์๋ ํจ๊ณผ์ ์ธ ๋ฐฉ๋ฒ์ด์ผ. ์๋ฒ ์์ ์ ํ ํ๋ฆฟ์ ํ ๋ฒ๋ง ํ์ฑํ๊ณ ๋ฉ๋ชจ๋ฆฌ์ ์บ์ฑํด๋๋ ๊ฑฐ์ง.
package main
import (
"html/template"
"log"
"net/http"
"sync"
)
// ์ ์ญ ํ
ํ๋ฆฟ ์บ์
var (
templates *template.Template
once sync.Once
)
// ํ
ํ๋ฆฟ ์ด๊ธฐํ (ํ ๋ฒ๋ง ์คํ๋จ)
func initTemplates() {
once.Do(func() {
var err error
templates, err = template.ParseGlob("templates/*.html")
if err != nil {
log.Fatal("ํ
ํ๋ฆฟ ํ์ฑ ์คํจ:", err)
}
log.Println("ํ
ํ๋ฆฟ ์บ์ฑ ์๋ฃ!")
})
}
func handler(w http.ResponseWriter, r *http.Request) {
// ์บ์ฑ๋ ํ
ํ๋ฆฟ ์ฌ์ฉ (ํ์ฑ ๋น์ฉ ์์!)
err := templates.ExecuteTemplate(w, "page.html", data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func main() {
initTemplates()
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
๐ก ๊ฐ๋ฐ ๋ชจ๋์์๋? ๊ฐ๋ฐ ์ค์๋ ํ ํ๋ฆฟ์ ์์ ํ ๋๋ง๋ค ์๋ฒ๋ฅผ ์ฌ์์ํ๊ธฐ ๊ท์ฐฎ์. ์ด๋ด ๋๋ ํ๊ฒฝ ๋ณ์๋ก ๊ฐ๋ฐ/ํ๋ก๋์ ๋ชจ๋๋ฅผ ๊ตฌ๋ถํด์, ๊ฐ๋ฐ ๋ชจ๋์์๋ ๋งค ์์ฒญ๋ง๋ค ํ ํ๋ฆฟ์ ๋ค์ ํ์ฑํ๋๋ก ํ ์ ์์ด!
package main
import (
"html/template"
"os"
)
var isDevelopment = os.Getenv("ENV") == "development"
func getTemplate(name string) (*template.Template, error) {
if isDevelopment {
// ๊ฐ๋ฐ ๋ชจ๋: ๋งค๋ฒ ์๋ก ํ์ฑ
return template.ParseFiles("templates/" + name)
}
// ํ๋ก๋์
: ์บ์ฑ๋ ํ
ํ๋ฆฟ ์ฌ์ฉ
return templates.Lookup(name), nil
}
2๏ธโฃ ๋ฒํผ ํ ์ฌ์ฉํ๊ธฐ
ํ
ํ๋ฆฟ์ ์คํํ ๋๋ง๋ค ์๋ก์ด ๋ฒํผ๋ฅผ ํ ๋นํ๋ฉด GC ๋ถ๋ด์ด ์ปค์ ธ. sync.Pool์ ์ฌ์ฉํด์ ๋ฒํผ๋ฅผ ์ฌ์ฌ์ฉํ๋ฉด ์ฑ๋ฅ์ด ํฌ๊ฒ ํฅ์๋ผ!
package main
import (
"bytes"
"html/template"
"net/http"
"sync"
)
var bufferPool = sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}
func renderTemplate(w http.ResponseWriter, name string, data interface{}) error {
// ํ์์ ๋ฒํผ ๊ฐ์ ธ์ค๊ธฐ
buf := bufferPool.Get().(*bytes.Buffer)
buf.Reset()
defer bufferPool.Put(buf) // ์ฌ์ฉ ํ ํ์ ๋ฐํ
// ๋ฒํผ์ ๋จผ์ ๋ ๋๋ง
err := templates.ExecuteTemplate(buf, name, data)
if err != nil {
return err
}
// ์๋ฌ๊ฐ ์์ผ๋ฉด HTTP ์๋ต์ผ๋ก ์ ์ก
_, err = buf.WriteTo(w)
return err
}
func handler(w http.ResponseWriter, r *http.Request) {
data := getData()
if err := renderTemplate(w, "page.html", data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
์ด ๋ฐฉ๋ฒ์ ์ฅ์ ์ ํ ํ๋ฆฟ ์คํ ์ค ์๋ฌ๊ฐ ๋ฐ์ํด๋ HTTP ์๋ต์ ๋ถ๋ถ์ ์ธ ๋ด์ฉ์ด ์ ์ก๋์ง ์๋๋ค๋ ๊ฑฐ์ผ. ๋ฒํผ์์ ์์ ํ ๋ ๋๋ง๋ ํ์๋ง ์ ์กํ๋๊น!
3๏ธโฃ ํ ํ๋ฆฟ ์ฌ์ ์ปดํ์ผ
๋ณต์กํ ํ
ํ๋ฆฟ์ ํ์ฑ ์๊ฐ์ด ์ค๋ ๊ฑธ๋ฆด ์ ์์ด. template.Must()๋ฅผ ์ฌ์ฉํด์ ์๋ฒ ์์ ์ ๋ชจ๋ ํ
ํ๋ฆฟ์ ๊ฒ์ฆํ๊ณ ์ปดํ์ผํด๋์.
var templates = template.Must(template.New("").Funcs(funcMap).ParseGlob("templates/*.html"))
// Must()๋ ์๋ฌ๊ฐ ์์ผ๋ฉด ํจ๋์ ์ผ์ผ์ผ์
// ์๋ฒ๊ฐ ์์๋๊ธฐ ์ ์ ํ
ํ๋ฆฟ ์ค๋ฅ๋ฅผ ์ก์ ์ ์์ด!
4๏ธโฃ ๋ถ๋ถ ๋ ๋๋ง ์ต์ํ
๋๋ฌด ๋ง์ ๋ถ๋ถ ํ ํ๋ฆฟ์ผ๋ก ์ชผ๊ฐ๋ฉด ์คํ๋ ค ์ฑ๋ฅ์ด ๋จ์ด์ง ์ ์์ด. ์ ์ ํ ๊ท ํ์ ์ฐพ๋ ๊ฒ ์ค์ํด!
โ๏ธ ๊ท ํ ์ก๊ธฐ
โ
์ข์ ์: ํค๋, ํธํฐ, ์ฌ์ด๋๋ฐ ๊ฐ์ ํฐ ๋จ์๋ก ๋ถ๋ฆฌ
โ ๋์ ์: ๋ฒํผ ํ๋, ๋งํฌ ํ๋๊น์ง ์ ๋ถ ๋ถ๋ถ ํ
ํ๋ฆฟ์ผ๋ก ๋ง๋ค๊ธฐ
5๏ธโฃ ๋ฐ์ดํฐ ์ค๋น ์ต์ ํ
ํ ํ๋ฆฟ์ ์ ๋ฌํ๋ ๋ฐ์ดํฐ๋ฅผ ๋ฏธ๋ฆฌ ๊ฐ๊ณตํด๋๋ฉด ํ ํ๋ฆฟ ์์์์ ์ฐ์ฐ์ ์ค์ผ ์ ์์ด.
// ๋์ ์: ํ
ํ๋ฆฟ ์์์ ๋ณต์กํ ๊ณ์ฐ
{{range .Items}}
{{if gt (len .Description) 100}}
{{slice .Description 0 100}}...
{{else}}
{{.Description}}
{{end}}
{{end}}
// ์ข์ ์: Go ์ฝ๋์์ ๋ฏธ๋ฆฌ ์ฒ๋ฆฌ
type Item struct {
Name string
Description string
ShortDesc string // ๋ฏธ๋ฆฌ ์๋ผ๋ ์งง์ ์ค๋ช
}
func prepareItems(items []Item) []Item {
for i := range items {
if len(items[i].Description) > 100 {
items[i].ShortDesc = items[i].Description[:100] + "..."
} else {
items[i].ShortDesc = items[i].Description
}
}
return items
}
// ํ
ํ๋ฆฟ์์๋ ๊ฐ๋จํ๊ฒ
{{range .Items}}
{{.ShortDesc}}
{{end}}
๐จ ์ปค์คํ ํจ์ ๋ง๋ค๊ธฐ
๊ธฐ๋ณธ ํ ํ๋ฆฟ ํจ์๋ง์ผ๋ก๋ ๋ถ์กฑํ ๋๊ฐ ์์ด. ๋คํํ Go๋ ์ปค์คํ ํจ์๋ฅผ ํ ํ๋ฆฟ์ ์ถ๊ฐํ ์ ์๋ ๊ธฐ๋ฅ์ ์ ๊ณตํด! ์ด๊ฑธ ํ์ฉํ๋ฉด ํ ํ๋ฆฟ์ด ํจ์ฌ ๊ฐ๋ ฅํด์ ธ. ๐ช
๐ ๊ธฐ๋ณธ ์ฌ์ฉ๋ฒ
template.FuncMap์ ์ฌ์ฉํด์ ํจ์๋ฅผ ๋ฑ๋กํ๊ณ , ํ
ํ๋ฆฟ์์ ํธ์ถํ ์ ์์ด.
package main
import (
"html/template"
"net/http"
"strings"
"time"
)
// ์ปค์คํ
ํจ์ ๋งต ์ ์
var funcMap = template.FuncMap{
"upper": strings.ToUpper,
"lower": strings.ToLower,
"formatDate": func(t time.Time) string {
return t.Format("2006๋
01์ 02์ผ")
},
"add": func(a, b int) int {
return a + b
},
"multiply": func(a, b int) int {
return a * b
},
}
func main() {
// ํจ์ ๋งต์ ํ
ํ๋ฆฟ์ ๋ฑ๋ก
tmpl := template.Must(
template.New("page").Funcs(funcMap).ParseFiles("page.html"),
)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{
"Name": "ํ๊ธธ๋",
"Date": time.Now(),
"Price": 1000,
"Qty": 5,
}
tmpl.Execute(w, data)
})
http.ListenAndServe(":8080", nil)
}
<!-- page.html -->
<h1>{{upper .Name}}</h1>
<!-- ์ถ๋ ฅ: ํ๊ธธ๋ -->
<p>๋ ์ง: {{formatDate .Date}}</p>
<!-- ์ถ๋ ฅ: ๋ ์ง: 2024๋
01์ 15์ผ -->
<p>์ด์ก: {{multiply .Price .Qty}}์</p>
<!-- ์ถ๋ ฅ: ์ด์ก: 5000์ -->
๐จ ์ค์ฉ์ ์ธ ์ปค์คํ ํจ์ ์์
์ค๋ฌด์์ ์์ฃผ ์ฐ์ด๋ ์ ์ฉํ ํจ์๋ค์ ๋ง๋ค์ด๋ณด์!
package main
import (
"fmt"
"html/template"
"strings"
"time"
)
var funcMap = template.FuncMap{
// 1. ์ซ์ ํฌ๋งทํ
(์ฒ ๋จ์ ์ฝค๋ง)
"formatNumber": func(n int) string {
str := fmt.Sprintf("%d", n)
var result []rune
for i, r := range str {
if i > 0 && (len(str)-i)%3 == 0 {
result = append(result, ',')
}
result = append(result, r)
}
return string(result)
},
// 2. ์๋ ์๊ฐ ํ์ (1์๊ฐ ์ , 2์ผ ์ ๋ฑ)
"timeAgo": func(t time.Time) string {
duration := time.Since(t)
if duration < time.Minute {
return "๋ฐฉ๊ธ ์ "
} else if duration < time.Hour {
return fmt.Sprintf("%d๋ถ ์ ", int(duration.Minutes()))
} else if duration < 24*time.Hour {
return fmt.Sprintf("%d์๊ฐ ์ ", int(duration.Hours()))
} else {
return fmt.Sprintf("%d์ผ ์ ", int(duration.Hours()/24))
}
},
// 3. ๋ฌธ์์ด ์๋ฅด๊ธฐ (๋ง์ค์ํ ์ถ๊ฐ)
"truncate": func(s string, length int) string {
if len(s) <= length {
return s
}
return s[:length] + "..."
},
// 4. ์์ ํ ๋๋์
(0์ผ๋ก ๋๋๊ธฐ ๋ฐฉ์ง)
"safeDivide": func(a, b float64) float64 {
if b == 0 {
return 0
}
return a / b
},
// 5. ๋ฐฐ์ด์ ํน์ ๊ฐ์ด ์๋์ง ํ์ธ
"contains": func(slice []string, item string) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
},
// 6. ์ฒซ ๊ธ์๋ง ๋๋ฌธ์๋ก
"capitalize": func(s string) string {
if len(s) == 0 {
return s
}
return strings.ToUpper(s[:1]) + strings.ToLower(s[1:])
},
// 7. ๊ธฐ๋ณธ๊ฐ ์ ๊ณต
"default": func(defaultVal, val interface{}) interface{} {
if val == nil || val == "" {
return defaultVal
}
return val
},
// 8. ์ง์/ํ์ ํ๋ณ
"isEven": func(n int) bool {
return n%2 == 0
},
}
์ด์ ํ ํ๋ฆฟ์์ ์ด๋ ๊ฒ ์ฌ์ฉํ ์ ์์ด:
<!-- ์ซ์ ํฌ๋งทํ
-->
<p>๊ฐ๊ฒฉ: {{formatNumber .Price}}์</p>
<!-- ์ถ๋ ฅ: ๊ฐ๊ฒฉ: 1,234,567์ -->
<!-- ์๋ ์๊ฐ -->
<p>{{timeAgo .CreatedAt}}</p>
<!-- ์ถ๋ ฅ: 2์๊ฐ ์ -->
<!-- ๋ฌธ์์ด ์๋ฅด๊ธฐ -->
<p>{{truncate .Description 50}}</p>
<!-- ์ถ๋ ฅ: ๊ธด ์ค๋ช
์ด ์๋ค๋ฉด 50์๊น์ง๋ง ํ์ํ๊ณ ... -->
<!-- ๊ธฐ๋ณธ๊ฐ -->
<p>์ด๋ฆ: {{default "์ต๋ช
" .Username}}</p>
<!-- Username์ด ๋น์ด์์ผ๋ฉด "์ต๋ช
" ์ถ๋ ฅ -->
<!-- ์กฐ๊ฑด๋ถ ์คํ์ผ -->
{{range $index, $item := .Items}}
<div class="{{if isEven $index}}even{{else}}odd{{end}}">
{{$item.Name}}
</div>
{{end}}
๐ก ํจ์ ์ฒด์ด๋
ํ์ดํ๋ผ์ธ์ ์ฌ์ฉํ๋ฉด ์ฌ๋ฌ ํจ์๋ฅผ ์ฐ๊ฒฐํ ์ ์์ด:
{{.Title | lower | capitalize}}
โ ์ ์ฒด๋ฅผ ์๋ฌธ์๋ก ๋ง๋ ํ ์ฒซ ๊ธ์๋ง ๋๋ฌธ์๋ก!
๐ ์ฌ๋ฅ๋ท์์ ํ์ฉํ๊ธฐ
์ฌ๋ฅ๋ท ๊ฐ์ ์ฌ๋ฅ ๊ณต์ ํ๋ซํผ์ ๋ง๋ ๋ค๋ฉด ์ด๋ฐ ์ปค์คํ ํจ์๋ค์ด ์ ๋ง ์ ์ฉํ ๊ฑฐ์ผ! ์๋ฅผ ๋ค์ด:
var funcMap = template.FuncMap{
// ์ฌ๋ฅ ์นดํ
๊ณ ๋ฆฌ ์์ด์ฝ ๋ฐํ
"categoryIcon": func(category string) string {
icons := map[string]string{
"๋์์ธ": "๐จ",
"ํ๋ก๊ทธ๋๋ฐ": "๐ป",
"๊ธ์ฐ๊ธฐ": "โ๏ธ",
"๋ฒ์ญ": "๐",
"์์
": "๐ต",
}
if icon, ok := icons[category]; ok {
return icon
}
return "๐"
},
// ํ์ ์ ๋ณ๋ก ํ์
"ratingStars": func(rating float64) template.HTML {
fullStars := int(rating)
halfStar := rating-float64(fullStars) >= 0.5
stars := strings.Repeat("โญ", fullStars)
if halfStar {
stars += "โจ"
}
return template.HTML(stars)
},
// ๊ฐ๊ฒฉ ๋ฒ์ ํ์
"priceRange": func(min, max int) string {
if min == max {
return fmt.Sprintf("%s์", formatNumber(min))
}
return fmt.Sprintf("%s์ ~ %s์", formatNumber(min), formatNumber(max))
},
}
<!-- ์ฌ๋ฅ๋ท ์ฌ๋ฅ ์นด๋ ํ
ํ๋ฆฟ -->
{{range .Talents}}
<div class="talent-card">
<h3>{{categoryIcon .Category}} {{.Title}}</h3>
<p>{{truncate .Description 100}}</p>
<div class="rating">{{ratingStars .Rating}}</div>
<div class="price">{{priceRange .MinPrice .MaxPrice}}</div>
<div class="meta">
<span>{{timeAgo .UpdatedAt}}</span>
<span>์กฐํ {{formatNumber .Views}}</span>
</div>
</div>
{{end}}
๐ช ์ค์ ํ๋ก์ ํธ: ๋ธ๋ก๊ทธ ์์คํ ๋ง๋ค๊ธฐ
์ด์ ๊น์ง ๋ฐฐ์ด ๋ด์ฉ์ ๋ชจ๋ ํ์ฉํด์ ์ค์ ๋ก ๋์ํ๋ ๋ธ๋ก๊ทธ ์์คํ ์ ๋ง๋ค์ด๋ณด์! ์ด ์์ ๋ ์ค๋ฌด์์ ๋ฐ๋ก ์์ฉํ ์ ์์ ๊ฑฐ์ผ. ๐
๐ ํ๋ก์ ํธ ๊ตฌ์กฐ
blog/
โโโ main.go
โโโ handlers/
โ โโโ home.go
โ โโโ post.go
โ โโโ admin.go
โโโ models/
โ โโโ post.go
โโโ templates/
โ โโโ layout.html
โ โโโ home.html
โ โโโ post.html
โ โโโ admin.html
โโโ static/
โโโ css/
โ โโโ style.css
โโโ js/
โโโ main.js
๐๏ธ ๋ชจ๋ธ ์ ์
// models/post.go
package models
import "time"
type Post struct {
ID int
Title string
Slug string
Content string
Summary string
Author string
Category string
Tags []string
Views int
CreatedAt time.Time
UpdatedAt time.Time
Published bool
}
type Comment struct {
ID int
PostID int
Author string
Content string
CreatedAt time.Time
}
// ๋ฉ๋ชจ๋ฆฌ ์ ์ฅ์ (์ค์ ๋ก๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฌ์ฉ)
var posts = []Post{
{
ID: 1,
Title: "Go ํ
ํ๋ฆฟ ์์ ์ ๋ณต",
Slug: "go-template-guide",
Content: "Go์ html/template ํจํค์ง๋...",
Summary: "Go ํ
ํ๋ฆฟ์ ๋ชจ๋ ๊ฒ์ ์์๋ด
๋๋ค.",
Author: "๊น๊ฐ๋ฐ",
Category: "ํ๋ก๊ทธ๋๋ฐ",
Tags: []string{"Go", "์น๊ฐ๋ฐ", "ํ
ํ๋ฆฟ"},
Views: 1523,
CreatedAt: time.Now().Add(-48 * time.Hour),
UpdatedAt: time.Now().Add(-24 * time.Hour),
Published: true,
},
// ๋ ๋ง์ ํฌ์คํธ...
}
func GetAllPosts() []Post {
return posts
}
func GetPostBySlug(slug string) *Post {
for i := range posts {
if posts[i].Slug == slug {
return &posts[i]
}
}
return nil
}
func GetRecentPosts(limit int) []Post {
if limit > len(posts) {
limit = len(posts)
}
return posts[:limit]
}
๐จ ๋ ์ด์์ ํ ํ๋ฆฟ
<!-- templates/layout.html -->
{{define "layout"}}
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{block "title" .}}๊ฐ๋ฐ ๋ธ๋ก๊ทธ{{end}}</title>
<link rel="stylesheet" href="/static/css/style.css">
</head>
<body>
<header>
<nav>
<div class="logo">
<a href="/">๐ Dev Blog</a>
</div>
<ul class="menu">
<li><a href="/">ํ</a></li>
<li><a href="/posts">ํฌ์คํธ</a></li>
<li><a href="/about">์๊ฐ</a></li>
<li><a href="/admin">๊ด๋ฆฌ</a></li>
</ul>
</nav>
</header>
<main>
{{template "content" .}}
</main>
<footer>
<p>ยฉ 2024 Dev Blog. Made with Go & html/template</p>
</footer>
<script src="/static/js/main.js"></script>
</body>
</html>
{{end}}
๐ ํ ํ์ด์ง ํ ํ๋ฆฟ
<!-- templates/home.html -->
{{define "title"}}ํ - ๊ฐ๋ฐ ๋ธ๋ก๊ทธ{{end}}
{{define "content"}}
<section class="hero">
<h1>ํ์ํฉ๋๋ค! ๐</h1>
<p>Go ์ธ์ด์ ์น ๊ฐ๋ฐ์ ๊ดํ ์ด์ผ๊ธฐ๋ฅผ ๋๋๋๋ค.</p>
</section>
<section class="recent-posts">
<h2>์ต๊ทผ ํฌ์คํธ ๐</h2>
<div class="post-grid">
{{range .RecentPosts}}
<article class="post-card">
<div class="post-meta">
<span class="category">{{categoryIcon .Category}} {{.Category}}</span>
<span class="date">{{timeAgo .CreatedAt}}</span>
</div>
<h3><a href="/post/{{.Slug}}">{{.Title}}</a></h3>
<p>{{truncate .Summary 150}}</p>
<div class="post-footer">
<span class="author">โ๏ธ {{.Author}}</span>
<span class="views">๐๏ธ {{formatNumber .Views}}</span>
</div>
<div class="tags">
{{range .Tags}}
<span class="tag">#{{.}}</span>
{{end}}
</div>
</article>
{{else}}
<p>์์ง ํฌ์คํธ๊ฐ ์์ต๋๋ค.</p>
{{end}}
</div>
</section>
<section class="stats">
<div class="stat-card">
<h3>{{formatNumber .TotalPosts}}</h3>
<p>์ด ํฌ์คํธ</p>
</div>
<div class="stat-card">
<h3>{{formatNumber .TotalViews}}</h3>
<p>์ด ์กฐํ์</p>
</div>
<div class="stat-card">
<h3>{{len .Categories}}</h3>
<p>์นดํ
๊ณ ๋ฆฌ</p>
</div>
</section>
{{end}}
๐ ํฌ์คํธ ์์ธ ํ์ด์ง
<!-- templates/post.html -->
{{define "title"}}{{.Post.Title}} - ๊ฐ๋ฐ ๋ธ๋ก๊ทธ{{end}}
{{define "content"}}
{{with .Post}}
<article class="post-detail">
<header class="post-header">
<div class="post-meta">
<span class="category">{{categoryIcon .Category}} {{.Category}}</span>
<span class="date">{{formatDate .CreatedAt}}</span>
</div>
<h1>{{.Title}}</h1>
<div class="post-info">
<span class="author">โ๏ธ {{.Author}}</span>
<span class="views">๐๏ธ {{formatNumber .Views}}</span>
<span class="updated">๐ {{timeAgo .UpdatedAt}} ์
๋ฐ์ดํธ</span>
</div>
<div class="tags">
{{range .Tags}}
<a href="/tag/{{.}}" class="tag">#{{.}}</a>
{{end}}
</div>
</header>
<div class="post-content">
{{.Content | markdown}}
</div>
<footer class="post-footer">
<div class="share-buttons">
<button onclick="share('twitter')">๐ฆ ํธ์ํฐ</button>
<button onclick="share('facebook')">๐ ํ์ด์ค๋ถ</button>
<button onclick="copyLink()">๐ ๋งํฌ ๋ณต์ฌ</button>
</div>
</footer>
</article>
<section class="comments">
<h2>๋๊ธ ๐ฌ</h2>
{{if .Comments}}
{{range .Comments}}
<div class="comment">
<div class="comment-header">
<strong>{{.Author}}</strong>
<span>{{timeAgo .CreatedAt}}</span>
</div>
<p>{{.Content}}</p>
</div>
{{end}}
{{else}}
<p>์ฒซ ๋๊ธ์ ๋จ๊ฒจ๋ณด์ธ์!</p>
{{end}}
<form class="comment-form" method="POST" action="/post/{{.Slug}}/comment">
<input type="text" name="author" placeholder="์ด๋ฆ" required>
<textarea name="content" placeholder="๋๊ธ์ ์
๋ ฅํ์ธ์..." required></textarea>
<button type="submit">๋๊ธ ์์ฑ</button>
</form>
</section>
<section class="related-posts">
<h2>๊ด๋ จ ํฌ์คํธ ๐</h2>
<div class="post-list">
{{range .RelatedPosts}}
<div class="post-item">
<h4><a href="/post/{{.Slug}}">{{.Title}}</a></h4>
<p>{{truncate .Summary 80}}</p>
</div>
{{end}}
</div>
</section>
{{end}}
{{end}}
๐ฎ ํธ๋ค๋ฌ ๊ตฌํ
// main.go
package main
import (
"blog/handlers"
"blog/models"
"fmt"
"html/template"
"log"
"net/http"
"strings"
"time"
)
var templates *template.Template
func init() {
funcMap := template.FuncMap{
"formatNumber": formatNumber,
"formatDate": formatDate,
"timeAgo": timeAgo,
"truncate": truncate,
"categoryIcon": categoryIcon,
"markdown": markdown,
}
templates = template.Must(
template.New("").Funcs(funcMap).ParseGlob("templates/*.html"),
)
}
func main() {
// ์ ์ ํ์ผ ์๋น
fs := http.FileServer(http.Dir("static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
// ๋ผ์ฐํธ ๋ฑ๋ก
http.HandleFunc("/", homeHandler)
http.HandleFunc("/post/", postHandler)
http.HandleFunc("/admin", adminHandler)
log.Println("์๋ฒ ์์: http://localhost:8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
posts := models.GetAllPosts()
data := struct {
RecentPosts []models.Post
TotalPosts int
TotalViews int
Categories []string
}{
RecentPosts: models.GetRecentPosts(6),
TotalPosts: len(posts),
TotalViews: calculateTotalViews(posts),
Categories: getCategories(posts),
}
renderTemplate(w, "home.html", data)
}
func postHandler(w http.ResponseWriter, r *http.Request) {
slug := strings.TrimPrefix(r.URL.Path, "/post/")
post := models.GetPostBySlug(slug)
if post == nil {
http.NotFound(w, r)
return
}
// ์กฐํ์ ์ฆ๊ฐ
post.Views++
data := struct {
Post *models.Post
Comments []models.Comment
RelatedPosts []models.Post
}{
Post: post,
Comments: models.GetCommentsByPostID(post.ID),
RelatedPosts: models.GetRelatedPosts(post.ID, 3),
}
renderTemplate(w, "post.html", data)
}
func renderTemplate(w http.ResponseWriter, name string, data interface{}) {
err := templates.ExecuteTemplate(w, "layout", data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Printf("ํ
ํ๋ฆฟ ์คํ ์๋ฌ: %v", err)
}
}
// ํฌํผ ํจ์๋ค
func formatNumber(n int) string {
str := fmt.Sprintf("%d", n)
// ์ฒ ๋จ์ ์ฝค๋ง ์ถ๊ฐ ๋ก์ง
return str
}
func formatDate(t time.Time) string {
return t.Format("2006๋
01์ 02์ผ")
}
func timeAgo(t time.Time) string {
duration := time.Since(t)
// ์๋ ์๊ฐ ๊ณ์ฐ ๋ก์ง
return "๋ฐฉ๊ธ ์ "
}
func truncate(s string, length int) string {
if len(s) <= length {
return s
}
return s[:length] + "..."
}
func categoryIcon(category string) string {
icons := map[string]string{
"ํ๋ก๊ทธ๋๋ฐ": "๐ป",
"๋์์ธ": "๐จ",
"๊ธ์ฐ๊ธฐ": "โ๏ธ",
}
if icon, ok := icons[category]; ok {
return icon
}
return "๐"
}
func markdown(content string) template.HTML {
// ์ค์ ๋ก๋ ๋งํฌ๋ค์ด ํ์ ์ฌ์ฉ
// ์ฌ๊ธฐ์๋ ๊ฐ๋จํ HTML๋ก ๋ณํ
return template.HTML(content)
}
func calculateTotalViews(posts []models.Post) int {
total := 0
for _, post := range posts {
total += post.Views
}
return total
}
func getCategories(posts []models.Post) []string {
categoryMap := make(map[string]bool)
for _, post := range posts {
categoryMap[post.Category] = true
}
categories := make([]string, 0, len(categoryMap))
for cat := range categoryMap {
categories = append(categories, cat)
}
return categories
}
๐ ์์ฑ!
์ด์ ์์ ํ ๋์ํ๋ ๋ธ๋ก๊ทธ ์์คํ
์ด ์์ฑ๋์์ด! ์ด ์ฝ๋๋ฅผ ๊ธฐ๋ฐ์ผ๋ก:
โ
๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๋ ์ถ๊ฐ
โ
์ฌ์ฉ์ ์ธ์ฆ ์์คํ
๊ตฌํ
โ
๊ฒ์ ๊ธฐ๋ฅ ์ถ๊ฐ
โ
RSS ํผ๋ ์์ฑ
โ
๊ด๋ฆฌ์ ํ์ด์ง ํ์ฅ
๋ฑ์ ๊ธฐ๋ฅ์ ์ถ๊ฐํ ์ ์์ด!
๐ ๋๋ฒ๊น ๊ณผ ์๋ฌ ์ฒ๋ฆฌ
ํ ํ๋ฆฟ ์์ ์ ํ๋ค ๋ณด๋ฉด ์์์น ๋ชปํ ์๋ฌ๋ฅผ ๋ง๋๊ฒ ๋ผ. ํจ๊ณผ์ ์ผ๋ก ๋๋ฒ๊น ํ๊ณ ์๋ฌ๋ฅผ ์ฒ๋ฆฌํ๋ ๋ฐฉ๋ฒ์ ์์๋ณด์! ๐
1๏ธโฃ ํ ํ๋ฆฟ ํ์ฑ ์๋ฌ
ํ ํ๋ฆฟ ๋ฌธ๋ฒ์ด ์๋ชป๋๋ฉด ํ์ฑ ๋จ๊ณ์์ ์๋ฌ๊ฐ ๋ฐ์ํด. ์ด๋ฐ ์๋ฌ๋ ์๋ฒ ์์ ์ ์ก์์ผ ํด.
// ๋์ ์: ์๋ฌ ๋ฌด์
templates, _ := template.ParseGlob("templates/*.html")
// ์ข์ ์: ์๋ฌ ์ฒ๋ฆฌ
templates, err := template.ParseGlob("templates/*.html")
if err != nil {
log.Fatalf("ํ
ํ๋ฆฟ ํ์ฑ ์คํจ: %v", err)
}
// ๋ ์ข์ ์: Must ์ฌ์ฉ (ํจ๋ ๋ฐ์)
templates := template.Must(template.ParseGlob("templates/*.html"))
2๏ธโฃ ์คํ ์ ์๋ฌ
ํ ํ๋ฆฟ ์คํ ์ค์๋ ์๋ฌ๊ฐ ๋ฐ์ํ ์ ์์ด. ์๋ฅผ ๋ค์ด ์กด์ฌํ์ง ์๋ ํ๋์ ์ ๊ทผํ๊ฑฐ๋, ํ์ ์ด ๋ง์ง ์์ ๋ ๋ง์ด์ผ.
func renderTemplate(w http.ResponseWriter, name string, data interface{}) {
// ๋ฒํผ๋ฅผ ์ฌ์ฉํด์ ์๋ฌ ๋ฐ์ ์ ๋ถ๋ถ ์๋ต ๋ฐฉ์ง
buf := new(bytes.Buffer)
err := templates.ExecuteTemplate(buf, name, data)
if err != nil {
// ๋ก๊ทธ ๊ธฐ๋ก
log.Printf("ํ
ํ๋ฆฟ ์คํ ์๋ฌ [%s]: %v", name, err)
// ์ฌ์ฉ์์๊ฒ ์น์ ํ ์๋ฌ ํ์ด์ง ํ์
http.Error(w, "ํ์ด์ง๋ฅผ ์์ฑํ๋ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค.",
http.StatusInternalServerError)
return
}
// ์๋ฌ๊ฐ ์์ผ๋ฉด ๋ฒํผ ๋ด์ฉ ์ ์ก
w.Header().Set("Content-Type", "text/html; charset=utf-8")
buf.WriteTo(w)
}
3๏ธโฃ ๋๋ฒ๊น ํ
๐ก ์ ์ฉํ ๋๋ฒ๊น
๊ธฐ๋ฒ๋ค
1. ํ
ํ๋ฆฟ ๋ณ์ ์ถ๋ ฅํ๊ธฐ
{{printf "%#v" .}} - ํ์ฌ ์ปจํ
์คํธ์ ๋ชจ๋ ๋ด์ฉ ์ถ๋ ฅ
{{printf "%T" .Value}} - ํ์
ํ์ธ
2. ์กฐ๊ฑด๋ถ ๋๋ฒ๊ทธ ์ ๋ณด
๊ฐ๋ฐ ๋ชจ๋์์๋ง ๋๋ฒ๊ทธ ์ ๋ณด๋ฅผ ํ์ํ๋๋ก ์ค์
3. ํ
ํ๋ฆฟ ์ฃผ์ ํ์ฉ
{{/* ๋๋ฒ๊ทธ: ์ฌ๊ธฐ๊น์ง ์ ์ ์๋ */}}
// ๊ฐ๋ฐ ๋ชจ๋ ํ๋๊ทธ
var isDev = os.Getenv("ENV") == "development"
// ํ
ํ๋ฆฟ์ ์ ๋ฌ
data := struct {
IsDev bool
// ๋ค๋ฅธ ๋ฐ์ดํฐ...
}{
IsDev: isDev,
}
// ํ
ํ๋ฆฟ์์ ์ฌ์ฉ
{{if .IsDev}}
<div class="debug-panel">
<h3>๋๋ฒ๊ทธ ์ ๋ณด</h3>
<pre>{{printf "%#v" .}}</pre>
</div>
{{end}}
4๏ธโฃ ์ปค์คํ ์๋ฌ ํ์ด์ง
์ฌ์ฉ์ ์นํ์ ์ธ ์๋ฌ ํ์ด์ง๋ฅผ ๋ง๋ค์ด๋ณด์:
<!-- templates/error.html -->
{{define "error"}}
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>์ค๋ฅ ๋ฐ์</title>
<style>
.error-page {
max-width: 600px;
margin: 100px auto;
text-align: center;
padding: 40px;
}
.error-code {
font-size: 72px;
font-weight: bold;
color: #e74c3c;
}
.error-message {
font-size: 24px;
margin: 20px 0;
color: #555;
}
</style>
</head>
<body>
<div class="error-page">
<div class="error-code">{{.Code}}</div>
<div class="error-message">{{.Message}}</div>
<p>{{.Description}}</p>
<a href="/">ํ์ผ๋ก ๋์๊ฐ๊ธฐ</a>
</div>
</body>
</html>
{{end}}
func showError(w http.ResponseWriter, code int, message, description string) {
w.WriteHeader(code)
data := struct {
Code int
Message string
Description string
}{
Code: code,
Message: message,
Description: description,
}
templates.ExecuteTemplate(w, "error", data)
}
// ์ฌ์ฉ ์
func handler(w http.ResponseWriter, r *http.Request) {
post := getPost(id)
if post == nil {
showError(w, 404, "ํ์ด์ง๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค",
"์์ฒญํ์ ํ์ด์ง๊ฐ ์กด์ฌํ์ง ์๊ฑฐ๋ ์ญ์ ๋์์ต๋๋ค.")
return
}
// ์ ์ ์ฒ๋ฆฌ...
}
๐ ํ๋ก๋์ ๋ฐฐํฌ ์ฒดํฌ๋ฆฌ์คํธ
๊ฐ๋ฐ์ด ๋๋๊ณ ์ค์ ์๋น์ค๋ฅผ ๋ฐฐํฌํ ๋ ํ์ธํด์ผ ํ ์ฌํญ๋ค์ ์ ๋ฆฌํด๋ดค์ด. ์ด๊ฑธ ์ฒดํฌํ๋ฉด ์์ ํ๊ฒ ๋ฐฐํฌํ ์ ์์ด! โ
๐ ๋ณด์
โ
๋ชจ๋ ์ฌ์ฉ์ ์
๋ ฅ ๊ฒ์ฆ
โ
CSRF ํ ํฐ ๊ตฌํ
โ
HTTPS ์ฌ์ฉ
โ
๋ฏผ๊ฐ ์ ๋ณด ํ๊ฒฝ๋ณ์ ์ฒ๋ฆฌ
โ
SQL ์ธ์ ์
๋ฐฉ์ด
โก ์ฑ๋ฅ
โ
ํ
ํ๋ฆฟ ์บ์ฑ ํ์ฑํ
โ
์ ์ ํ์ผ ์์ถ
โ
CDN ์ฌ์ฉ
โ
๋ฐ์ดํฐ๋ฒ ์ด์ค ์ธ๋ฑ์ฑ
โ
์๋ต ์๊ฐ ๋ชจ๋ํฐ๋ง
๐ ์๋ฌ ์ฒ๋ฆฌ
โ
๋ชจ๋ ์๋ฌ ๋ก๊น
โ
์ฌ์ฉ์ ์นํ์ ์๋ฌ ํ์ด์ง
โ
ํจ๋ ๋ณต๊ตฌ ๋ฏธ๋ค์จ์ด
โ
ํฌ์ค ์ฒดํฌ ์๋ํฌ์ธํธ
โ
์๋ฌ ์๋ฆผ ์์คํ
๐ ๋ชจ๋ํฐ๋ง
โ
๋ก๊ทธ ์์ง ์์คํ
โ
๋ฉํธ๋ฆญ ์์ง
โ
์๋ฆผ ์ค์
โ
๋ฐฑ์
์๋ํ
โ
์ฑ๋ฅ ํ๋กํ์ผ๋ง
// ํ๋ก๋์
์ค์ ์์
package main
import (
"log"
"net/http"
"os"
"time"
)
func main() {
// ํ๊ฒฝ ๋ณ์์์ ์ค์ ์ฝ๊ธฐ
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
// ๋ก๊ฑฐ ์ค์
logFile, err := os.OpenFile("app.log",
os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Fatal(err)
}
defer logFile.Close()
log.SetOutput(logFile)
// ์๋ฒ ์ค์
srv := &http.Server{
Addr: ":" + port,
Handler: setupRoutes(),
ReadTimeout: 15 * time.Second,
WriteTimeout: 15 * time.Second,
IdleTimeout: 60 * time.Second,
}
// Graceful shutdown
log.Printf("์๋ฒ ์์: ํฌํธ %s", port)
if err := srv.ListenAndServe(); err != nil {
log.Fatal(err)
}
}
๐ก ์ค๋ฌด ํ๊ณผ ๋ฒ ์คํธ ํ๋ํฐ์ค
๋ง์ง๋ง์ผ๋ก ์ค๋ฌด์์ ์ ๋ง ์ ์ฉํ ํ๋ค์ ๊ณต์ ํ ๊ฒ. ์ด๊ฑด ๋ด๊ฐ ์ง์ ๊ฒฝํํ๋ฉด์ ๋ฐฐ์ด ๊ฒ๋ค์ด์ผ! ๐
ํ ํ๋ฆฟ ํ์ผ ๊ตฌ์กฐํ
ํ๋ก์ ํธ๊ฐ ์ปค์ง๋ฉด ํ ํ๋ฆฟ ํ์ผ๋ ๋ง์์ ธ. ์ฒด๊ณ์ ์ผ๋ก ๊ด๋ฆฌํ๋ ๊ฒ ์ค์ํด:
templates/
โโโ layouts/
โ โโโ base.html
โ โโโ admin.html
โโโ partials/
โ โโโ header.html
โ โโโ footer.html
โ โโโ sidebar.html
โโโ pages/
โ โโโ home.html
โ โโโ about.html
โ โโโ contact.html
โโโ components/
โโโ card.html
โโโ button.html
โโโ form.html
๋ฐ์ดํฐ ๊ตฌ์กฐ ๋ช ํํ ํ๊ธฐ
ํ ํ๋ฆฟ์ ์ ๋ฌํ๋ ๋ฐ์ดํฐ๋ ๋ช ํํ ๊ตฌ์กฐ์ฒด๋ก ์ ์ํด. ๋งต๋ณด๋ค๋ ๊ตฌ์กฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ ๊ฒ ์ข์:
// ๋์ ์
data := map[string]interface{}{
"title": "์ ๋ชฉ",
"items": items,
// ํ์
์์ ์ฑ ์์
}
// ์ข์ ์
type PageData struct {
Title string
Items []Item
CurrentUser *User
Meta PageMeta
}
data := PageData{
Title: "์ ๋ชฉ",
Items: items,
// ํ์
์์ , IDE ์๋์์ฑ ์ง์
}
ํ ํ๋ฆฟ ํ ์คํธ ์์ฑ
ํ ํ๋ฆฟ๋ ํ ์คํธํ ์ ์์ด! ์ค์ํ ํ ํ๋ฆฟ์ ๊ผญ ํ ์คํธ๋ฅผ ์์ฑํ์:
func TestHomeTemplate(t *testing.T) {
tmpl := template.Must(template.ParseFiles("templates/home.html"))
data := PageData{
Title: "ํ
์คํธ",
Items: []Item{{Name: "์์ดํ
1"}},
}
var buf bytes.Buffer
err := tmpl.Execute(&buf, data)
if err != nil {
t.Fatalf("ํ
ํ๋ฆฟ ์คํ ์คํจ: %v", err)
}
output := buf.String()
if !strings.Contains(output, "ํ
์คํธ") {
t.Error("์ ๋ชฉ์ด ์ถ๋ ฅ๋์ง ์์")
}
}
์ฌ๋ฅ๋ท์ฒ๋ผ ์ปดํฌ๋ํธ ์ฌ์ฌ์ฉํ๊ธฐ
์ฌ๋ฅ๋ท ๊ฐ์ ํ๋ซํผ์ ๋ง๋ค ๋๋ ์ฌ์ฌ์ฉ ๊ฐ๋ฅํ ์ปดํฌ๋ํธ๋ฅผ ๋ง์ด ๋ง๋ค์ด๋๋ฉด ๊ฐ๋ฐ ์๋๊ฐ ๋นจ๋ผ์ ธ:
<!-- components/talent-card.html -->
{{define "talent-card"}}
<div class="talent-card">
<img src="{{.Image}}" alt="{{.Title}}">
<h3>{{.Title}}</h3>
<p>{{truncate .Description 100}}</p>
<div class="price">{{formatNumber .Price}}์</div>
<div class="rating">{{ratingStars .Rating}}</div>
</div>
{{end}}
<!-- ์ฌ์ฉ -->
{{range .Talents}}
{{template "talent-card" .}}
{{end}}
์ฑ๋ฅ ํ๋กํ์ผ๋ง
ํ ํ๋ฆฟ ๋ ๋๋ง ์ฑ๋ฅ์ ์ธก์ ํ๊ณ ์ต์ ํํ์:
import (
"runtime/pprof"
"time"
)
func benchmarkTemplate(name string, data interface{}) {
start := time.Now()
var buf bytes.Buffer
templates.ExecuteTemplate(&buf, name, data)
duration := time.Since(start)
log.Printf("ํ
ํ๋ฆฟ %s ๋ ๋๋ง ์๊ฐ: %v", name, duration)
}
๐ ๋ง๋ฌด๋ฆฌํ๋ฉฐ
์! ์ฌ๊ธฐ๊น์ง ์ฝ์๋ค๋ฉด ์ ๋ง ๋๋จํด! ๐
Go์ html/template ํจํค์ง๋ ์ฒ์์๋ ๋ณต์กํด ๋ณด์ผ ์ ์์ง๋ง, ์ต์ํด์ง๋ฉด ์ ๋ง ๊ฐ๋ ฅํ ๋๊ตฌ์ผ. ์๋ ์ด์ค์ผ์ดํ์ผ๋ก ๋ณด์์ ์ฑ๊ธฐ๋ฉด์๋, ์ ์ฐํ ํ
ํ๋ฆฟ ๋ฌธ๋ฒ์ผ๋ก ๋ณต์กํ UI๋ ์ฝ๊ฒ ๋ง๋ค ์ ์๊ฑฐ๋ .
์ด ๊ธ์์ ๋ค๋ฃฌ ๋ด์ฉ์ ์ ๋ฆฌํ๋ฉด:
๐ ํต์ฌ ์์ฝ
โ
๊ธฐ๋ณธ ๊ฐ๋
: ํ
ํ๋ฆฟ ์ ์, ํ์ฑ, ์คํ์ 3๋จ๊ณ
โ
๋ฌธ๋ฒ: ๋ณ์ ์ถ๋ ฅ, ์กฐ๊ฑด๋ฌธ, ๋ฐ๋ณต๋ฌธ, ํ์ดํ๋ผ์ธ
โ
ํ
ํ๋ฆฟ ์์: define, block, template์ผ๋ก ์ฌ์ฌ์ฉ
โ
๋ณด์: ์๋ ์ด์ค์ผ์ดํ๊ณผ XSS ๋ฐฉ์ด
โ
์ฑ๋ฅ: ์บ์ฑ, ๋ฒํผ ํ, ์ต์ ํ ์ ๋ต
โ
์ปค์คํ
ํจ์: FuncMap์ผ๋ก ๊ธฐ๋ฅ ํ์ฅ
โ
์ค์ ํ๋ก์ ํธ: ๋ธ๋ก๊ทธ ์์คํ
๊ตฌํ
โ
๋๋ฒ๊น
: ์๋ฌ ์ฒ๋ฆฌ์ ๋๋ฒ๊น
๊ธฐ๋ฒ
โ
๋ฒ ์คํธ ํ๋ํฐ์ค: ์ค๋ฌด ํ๊ณผ ์ฒดํฌ๋ฆฌ์คํธ
์ด์ ๋น์ ๋ Go๋ก ๋ฉ์ง ๋์ ์น ํ์ด์ง๋ฅผ ๋ง๋ค ์ ์์ด! ๐
์ฒ์์๋ ์์ ํ๋ก์ ํธ๋ถํฐ ์์ํด๋ด. ๊ฐ๋จํ ๋ธ๋ก๊ทธ๋ ํฌํธํด๋ฆฌ์ค ์ฌ์ดํธ๋ฅผ ๋ง๋ค๋ฉด์ ์ตํ๋ ๊ฒ ๊ฐ์ฅ ์ข์. ๊ทธ๋ฆฌ๊ณ ์ ์ ๋ณต์กํ ๊ธฐ๋ฅ์ ์ถ๊ฐํด๊ฐ๋ฉด์ ์ค๋ ฅ์ ํค์๋๊ฐ๋ฉด ๋ผ.
ํน์ ๋งํ๋ ๋ถ๋ถ์ด ์๋ค๋ฉด Go ๊ณต์ ๋ฌธ์๋ ์ปค๋ฎค๋ํฐ๋ฅผ ํ์ฉํด๋ด. Go ๊ฐ๋ฐ์ ์ปค๋ฎค๋ํฐ๋ ์ ๋ง ์น์ ํ๊ณ ๋์์ ๋ง์ด ์ค. ๐
๊ทธ๋ฆฌ๊ณ ์ฌ๋ฅ ๊ณต์ ๊ฐ ํ์ํ๋ค๋ฉด ์ฌ๋ฅ๋ท์์ Go ๊ฐ๋ฐ ์ ๋ฌธ๊ฐ๋ค์ ๋ง๋๋ณผ ์๋ ์์ด. ์ค๋ฌด ๊ฒฝํ์ด ํ๋ถํ ๊ฐ๋ฐ์๋ค์๊ฒ ์ง์ ๋ฐฐ์ฐ๋ฉด ํจ์ฌ ๋น ๋ฅด๊ฒ ์ฑ์ฅํ ์ ์๊ฑฐ๋ !
์, ์ด์ ์ฝ๋ ์๋ํฐ๋ฅผ ์ด๊ณ ์ง์ ๋ง๋ค์ด๋ณด์! ๐ปโจ
ํ์ด์ ๋น์ด! ํ์ดํ
! ๐ฏ
๊ด๋ จ ํค์๋
๋๊ธ ์์ฑ
์ด ๊ธ์ ๋ํ ์ฌ๋ฌ๋ถ์ ์๊ฐ์ ๋ค๋ ค์ฃผ์ธ์
๋ก๊ทธ์ธ์ด ํ์ํฉ๋๋ค
๋๊ธ์ ์์ฑํ๋ ค๋ฉด ๋จผ์ ๋ก๊ทธ์ธํด์ฃผ์ธ์.
๋๊ธ 0
์ง์์ธ์ ์ฒ - ์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
- ์ ์๊ถ ๋ฐ ์์ ๊ถ: ๋ณธ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ๋ ์ AI ๊ธฐ์ ๋ก ์์ฑ๋์์ผ๋ฉฐ, ๋ํ๋ฏผ๊ตญ ์ ์๊ถ๋ฒ ๋ฐ ๊ตญ์ ์ ์๊ถ ํ์ฝ์ ์ํด ๋ณดํธ๋ฉ๋๋ค.
- AI ์์ฑ ์ปจํ ์ธ ์ ๋ฒ์ ์ง์: ๋ณธ AI ์์ฑ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ์ง์ ์ฐฝ์๋ฌผ๋ก ์ธ์ ๋๋ฉฐ, ๊ด๋ จ ๋ฒ๊ท์ ๋ฐ๋ผ ์ ์๊ถ ๋ณดํธ๋ฅผ ๋ฐ์ต๋๋ค.
- ์ฌ์ฉ ์ ํ: ์ฌ๋ฅ๋ท์ ๋ช ์์ ์๋ฉด ๋์ ์์ด ๋ณธ ์ปจํ ์ธ ๋ฅผ ๋ณต์ , ์์ , ๋ฐฐํฌ, ๋๋ ์์ ์ ์ผ๋ก ํ์ฉํ๋ ํ์๋ ์๊ฒฉํ ๊ธ์ง๋ฉ๋๋ค.
- ๋ฐ์ดํฐ ์์ง ๊ธ์ง: ๋ณธ ์ปจํ ์ธ ์ ๋ํ ๋ฌด๋จ ์คํฌ๋ํ, ํฌ๋กค๋ง, ๋ฐ ์๋ํ๋ ๋ฐ์ดํฐ ์์ง์ ๋ฒ์ ์ ์ฌ์ ๋์์ด ๋ฉ๋๋ค.
- AI ํ์ต ์ ํ: ์ฌ๋ฅ๋ท์ AI ์์ฑ ์ปจํ ์ธ ๋ฅผ ํ AI ๋ชจ๋ธ ํ์ต์ ๋ฌด๋จ ์ฌ์ฉํ๋ ํ์๋ ๊ธ์ง๋๋ฉฐ, ์ด๋ ์ง์ ์ฌ์ฐ๊ถ ์นจํด๋ก ๊ฐ์ฃผ๋ฉ๋๋ค.
์์ง ๋๊ธ์ด ์์ต๋๋ค.