4

Go 1.8 Template Execute Output Inconsistent

 2 years ago
source link: http://siongui.github.io/2017/02/21/go-1.8-template-execute-output-inconsistent/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Go 1.8 Template Execute Output Inconsistent

Updated: April 08, 2017

Update: This issue seems fixed in Go 1.8.1.

I found a bug in Go 1.8 template package (text/template & html/template), which output inconsistent HTML after ExecuteTemplate. Sometimes I get correct HTML output, and sometimes not. After I downgrade the Go version to 1.7.5, everything works fine without problem.

The tempaltes (total 4 files) I use:

theme/template/layout/layout.html

{{- define "layout" -}}
<!doctype html>
<html prefix="og: http://ogp.me/ns#">
<head>
  <meta charset="utf-8">
  <title>วัดป่าโพธิญาณ</title>
  <meta name="description" content="วัดป่าโพธิญาณ - สาขา 8 วัดหนองป่าพง">
  <meta name="keywords" content="วัดเขื่อน">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  {{template "metaog" .}}
</head>
<body>

{{block "content" .}}{{end}}

{{template "footer" .}}
</body>
</html>
{{- end -}}

theme/template/layout/includes/metaog.html

{{- define "metaog" -}}
  <meta property="og:title" content="วัดป่าโพธิญาณ">
  <meta property="og:type" content="website">
  <meta property="og:description" content="วัดป่าโพธิญาณ - สาขา 8 วัดหนองป่าพง">
  <meta property="og:image" content="{{.OgImage}}">
  <meta property="og:url" content="{{.OgUrl}}">
  <meta property="og:locale" content="{{.OgLocale}}">
  <meta property="og:locale:alternate" content="en_US">
  <meta property="og:locale:alternate" content="zh_TW">
{{- end -}}

theme/template/layout/includes/footer.html

{{- define "footer" -}}
<div>Powered by
  <a href="https://golang.org/">Go</a>
</div>
{{- end -}}

theme/template/index.html

{{- template "layout" .}}
{{define "content" -}}
<div>Hello World!</div>
<div>Template Inheritance in Go html/template</div>
{{- end -}}

The correct HTML output should be:

<!doctype html>
<html prefix="og: http://ogp.me/ns#">
<head>
  <meta charset="utf-8">
  <title>วัดป่าโพธิญาณ</title>
  <meta name="description" content="วัดป่าโพธิญาณ - สาขา 8 วัดหนองป่าพง">
  <meta name="keywords" content="วัดเขื่อน">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta property="og:title" content="วัดป่าโพธิญาณ">
  <meta property="og:type" content="website">
  <meta property="og:description" content="วัดป่าโพธิญาณ - สาขา 8 วัดหนองป่าพง">
  <meta property="og:image" content="https://upload.wikimedia.org/wikipedia/commons/d/df/Dharma_Wheel.svg">
  <meta property="og:url" content="https://siongui.github.io/watpah/">
  <meta property="og:locale" content="th_TH">
  <meta property="og:locale:alternate" content="en_US">
  <meta property="og:locale:alternate" content="zh_TW">
</head>
<body>

<div>Hello World!</div>
<div>Template Inheritance in Go html/template</div>

<div>Powered by
  <a href="https://golang.org/">Go</a>
</div>
</body>
</html>

Sometime I get the following output in Go 1.8 template package:

<!doctype html>
<html prefix="og: http://ogp.me/ns#">
<head>
  <meta charset="utf-8">
  <title>วัดป่าโพธิญาณ</title>
  <meta name="description" content="วัดป่าโพธิญาณ - สาขา 8 วัดหนองป่าพง">
  <meta name="keywords" content="วัดเขื่อน">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta property="og:title" content="วัดป่าโพธิญาณ">
  <meta property="og:type" content="website">
  <meta property="og:description" content="วัดป่าโพธิญาณ - สาขา 8 วัดหนองป่าพง">
  <meta property="og:image" content="https://upload.wikimedia.org/wikipedia/commons/d/df/Dharma_Wheel.svg">
  <meta property="og:url" content="https://siongui.github.io/watpah/">
  <meta property="og:locale" content="th_TH">
  <meta property="og:locale:alternate" content="en_US">
  <meta property="og:locale:alternate" content="zh_TW">
</head>
<body>



<div>Powered by
<a href="https://golang.org/">Go</a>
</div>
</body>
</html>

Interestingly, some one report that there is some problem with Go 1.8 sync/atomic package [2]. Not sure whether it's the same issue or not?

References:

[1]generate html from template · siongui/wat-pah-photiyan@a872d70 · GitHub


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK