Why is 'title' an unexported field of struct?

I am getting this error:

template: header.gohtml:6:13: executing “header” at <.title>: title is an unexported field of struct type main.pageData

main.go

type pageData struct {
	title string
	firstname string
}

func apply(w http.ResponseWriter, r *http.Request) {
	pd := pageData{title: "Apply"}
	var firstname string
	if r.Method == http.MethodPost {
		firstname = r.FormValue("firstname")
		pd.firstname = firstname
	}
}

templates/apply.gohtml

{{template "header" .}}
<h1>APPLY</h1>
{{if .title}}
Your name is {{.title}}
{{end}}
{{template "nav-main"}}
{{template "footer"}}

templates/includes/header.gohtml

{{ define "header"}}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{.title}}</title>
    <link rel="stylesheet" href="/public">
</head>
<body>
{{end}}

What is wrong with my code?

“export” means “public” => with upper case first letter
the reason is simple: the renderer package use the reflect package in order to get/set fields values
the reflect package can only access public/exported struct fields.
So try defining

type pageData struct {
	Title string
	Firstname string
}
4 Likes

Wow I am glad to know this. Thank you very much!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.