[Gin][Go] 페이지 이동하기

PersesTitan·2022년 7월 3일
0

Gin

목록 보기
6/7

저는 여기서 만든 메인페이지에서 about 페이지로 이동할 생각입니다.

footer.html

{{define "footer.html"}}
</div>
</body>
</html>
{{end}}

header.html

{{define "header.html"}}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" type="text">
  <title></title>
</head>
<body>
<div class="container">
{{end}}

index.html

{{template "header.html"}}
<h1 class="title">{{.title}}</h1>
<a href="{{.url}}">about으로 이동</a>
{{template "footer.html"}}

about.html

{{template "header.html"}}
<h1>about Page</h1>
<p>{{ .content}}</p>
{{template "footer.html"}}

main.go

package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

func setRouter(router *gin.Engine) {
	router.GET("/", func(c *gin.Context) {
		c.HTML(http.StatusOK, "index.html", gin.H{
			"title": "메인페이지",
			"url":   "/about",
		})
	})

	router.GET("/about", func(c *gin.Context) {
		c.HTML(http.StatusOK, "about.html", gin.H{
			"content": "내용",
		})
	})
}

func main() {
	router := gin.Default()
	router.LoadHTMLGlob("templates/*.html")
	setRouter(router)
	_ = router.Run(":8080")
}


이제 링크가 생겼습니다.


짠...! 링크를 누르면 about페이지로 이동할수 있습니다.


깃허브 링크

profile
안녕하세요 페르세스 티탄입니다! 부족한 부분이 많이 있겠지만 잘부탁드립니다.

0개의 댓글