Go templates

Go has its own template engine that is split into two packages: text/template and html/template. These packages are similar in functionality, with the difference that html/template allows a user to generate HTML code that is safe against code injection, making it suitable for use on web pages and emails. Also, these packages provide coding assistance in other applications such as the configuration for Helm and the Kubernetes package manager.

To specify mapping of a type between a Go template and an application, GoLand uses the gotype comment of the following structure: {{- /*gotype: package/path.type_name*/ -}}.

For Go templates, the commonly used file extensions are:

  • .gohtml: specifically indicates a Go HTML template, useful if your editor or IDE provides specialized support for these templates. GoLand has association with this extension.

  • .tmpl: a general extension for template files, suitable for Go templates.

  • .tpl: another general extension for template files used in Go.

Define mapping of a type between Go template and application

  1. Add an HTML tag (for example, <title></title>).

  2. Inside an HTML tag, type {{.}}.

  3. Place the caret after the dot, press AltEnter, and select Specify dot type.

  4. In the gotype comment section, select the necessary type from the code completion popup CtrlSpace.

Files on GitHub.

Alternatively, type {{- /*gotype: */ -}}, place the caret after gotype:, press CtrlSpace, and select the necessary type.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值