In addition to content quality and user experience, there is another important factor in whether a website is search engine friendly, namely TDK setup. TDK is the acronym of title, keywords, and Description. In this section, we’ll show you how to use Golang to set up TDK for your blog.

The TDK of the article has input boxes for filling in when we publish the article, which can be saved in the database. However, the home page and list page need to be set manually. The TDK information on the home page is also the most important part of SEO optimization. We need to set up TDK information reasonably for each page of the blog.

Define the TDK structure information for the page

We add a WebInfo struct structure to the controller/common.go file and declare use of it:

type WebInfo struct {
	Title       string `json:"title"`
	Keywords    string `json:"keywords"`
	Description string `json:"description"`
	NavBar      uint   `json:"nav_bar"`
}

var webInfo WebInfo
Copy the code

Structure information includes Title, Keywords, Description and NavBar, which correspond to:

  • TitleCorresponding to the current page SEO title, often, page SEO title and show the user to see the title will be slightly different, such as increasing the site suffix. SEO headings that contain multiple separated phrases are recommended to use underscores_Link them up, like “Golang Practical Tutorial _iris blog”. Example:Search external network: SEO training introduction graphic tutorial, network marketing technology video network course
  • KeywordsCorresponding to the page SEO keywords, multiple keywords using English comma.Separated. Keywords should not be too much, generally a page set three to five keywords, and these keywords are best part of the title, keywords also exist in the page. When setting keywords, the big words need to be optimized in front of the small words, the page should also pay attention to the density of keywords, too high keyword density will form keyword stacking, too little keyword density will not optimize the effect. Example:SEO,SEO training,SEO tutorials
  • DescriptionCorresponds to the description of the current page. Descriptions are often presented in the form of a one-sentence explanation. Example:Sownet focus on SEO training and network marketing technology video network course. To provide users with SEO site-building system soouwai 6 system, small program design soouwai tangram, didi friend chain and other tools platform.
  • NavBarThis field is mainly used to set the selection effect in the navigation section.

Call TDK information in the front page

The CORRESPONDING HTML tags of TDK information are

tags,
tags, and
tags.

As a result, we open the template/partials/header. The HTML, inside the head tag, increase the three tabs:

<title>{% if webInfo.Title %}{{webInfo.Title}}_{% endif %}{{SiteName}}</title>
{% if webInfo.Keywords %}<meta name="keywords" content="{{webInfo.Keywords}}">{% endif %}
{% if webInfo.Description %}<meta name="description" content="{{webInfo.Description}}">{% endif %}
Copy the code

The title tag is displayed on each page, and in addition to displaying the webInfo.title setting, it is followed by SiteName, the small tail of the web site.

The keywords tag is not required, so let’s make it display if there is, and not display if there is.

The same is true for the description tag, which is not displayed if no description information is set.

Set TDK information for each page that needs to be captured by search engines

Having defined the structural content of the TDK information above, we now add them individually to each controller.

Home controllerIndexPage():

In order to conveniently set the TDK information of the home page, we put the TDK information of the home page into config.json, the server part, and extend the server information:

/ /...
"server": {
  "site_name": "See your blog"."env": "development"."port": 8001."log_level": "debug"."title": "See your blog: Golang combat development introduction graphic tutorial, Golang combat technology online learning website"."keywords": "Golang, Golang combat development, Golang combat learning."."description": "There are many golang development tutorials online, and they all start from the basics of the installation environment, golang syntax, for a little white, it is helpful. However, as we all know, blindly accept learning, is a boring to suffocating process. More often, learning is over, should forget, should not forget most of the forgotten, not according to their actual needs to learn, most of the memory is not deep enough. In order to break this inefficient learning process, I will start from here and introduce a golang practical learning method with demand learning."."icp": "Guangdong ICP 19130249-2"
}
/ /...
Copy the code

In config/server.go, extend the server structure:

type serverConfig struct {
	SiteName    string `json:"site_name"`
	Env         string `json:"env"`
	Port        int    `json:"port"`
	LogLevel    string `json:"log_level"`
	Title       string `json:"title"`
	Keywords    string `json:"keywords"`
	Description string `json:"description"`
	Icp         string `json:"icp"`
}
Copy the code

Next, we add the calling code to IndexPage() :

webTitle := config.ServerConfig.Title
	ifcategory ! =nil {
		webTitle += "_"+ category.Title webInfo.NavBar = category.Id } webInfo.Title = webTitle webInfo.Keywords = config.ServerConfig.Keywords  webInfo.Description = config.ServerConfig.Description ctx.ViewData("webInfo", webInfo)
Copy the code

The title setting is called by default on the home page. Category headings are appended if category filtering conditions exist. Home page description, home keywords are called one by one.

Article details controllerArticleDetail()

webInfo.Title = article.Title
webInfo.Keywords = article.Keywords
webInfo.Description = article.Description
ctx.ViewData("webInfo", webInfo)
Copy the code

In the article details page, we can directly call the TDK information filled in the article.

Other pages have been added for completeness.

Install(ctx iris.Context)

webInfo.Title = "Blog Initialization"
ctx.ViewData("webInfo", webInfo)
Copy the code

AdminLogin(ctx iris.Context)

	webInfo.Title = "Login"
	ctx.ViewData("webInfo", webInfo)
Copy the code

At this point, TDK information is set up. Let’s restart the project to check the TDK information. If it is normal, it means that the information is correct.

The complete project sample code is hosted on GitHub. The complete project code can be viewed at github.com/fesiong/gob… You can also fork a copy to make changes on it.