# Setting up a Go template code formatter in VS Code

**URL:** <https://discourse.gohugo.io/t/setting-up-a-go-template-code-formatter-in-vs-code/37155>\
**Category:** tips & tricks\
**Created:** [February 14, 2022, 9:20am UTC](https://discourse.gohugo.io/t/setting-up-a-go-template-code-formatter-in-vs-code/37155 "2022-02-14T09:20:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [February 14, 2022, 9:20am UTC](https://discourse.gohugo.io/t/setting-up-a-go-template-code-formatter-in-vs-code/37155/1 "2022-02-14T09:20:03Z")

</div>

See [Setting up tempate formatting with prettier-plugin-go-template in VS Code · Issue #1656 · gohugoio/hugoDocs · GitHub](https://github.com/gohugoio/hugoDocs/issues/1656)

---

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [February 14, 2022, 1:48pm UTC](https://discourse.gohugo.io/t/setting-up-a-go-template-code-formatter-in-vs-code/37155/2 "2022-02-14T13:48:33Z")

</div>

The thing with this is, that it breaks a lot of special formats. In my specific pet-peeve case it will “crazy format” any JSON template. So I made it my habit to check in the footer of VSCode in all non-HTML-templates to select “plain text” so Prettier is not messing up the templates. Good for us, that VSCode lets us undo even pre-save-state, so nothing is too broken, but that prettier solution works only on HTML files that not only end on HTML but also _are_ actual HTML formats. (note: yep, my json template ends in HTML, and yep, I know I could rename it to something JSON-related).

I wish there would be some form of “config-comment” that tells VSCode how to treat a file.

I tried playing around with disabling Prettier and enabling several Golang plugins, but the point, in the end, is that VSCode sets the filetype by file extension and if it’s .html you won’t be able to mix things up. It has no way to describe the content type in any way.

Not ranting, just noting the limits of it. Setting those files to .json-endings will format nicely, but then the errors about wrong JSON-formatting are starting.

---

<div class="post-metadata">

**Author:** ![coliff](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/coliff/32/6832_2.png) [@coliff](https://discourse.gohugo.io/u/coliff)\
**Post date:** [February 14, 2022, 2:01pm UTC](https://discourse.gohugo.io/t/setting-up-a-go-template-code-formatter-in-vs-code/37155/4 "2022-02-14T14:01:32Z")

</div>

This sounds like more of an issue with Prettier rather than VS Code. I think you may be able to deal with your particular issue with a custom `.prettierrc` config if you want to adjust how Prettier works on certain files/folders.

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [February 14, 2022, 2:02pm UTC](https://discourse.gohugo.io/t/setting-up-a-go-template-code-formatter-in-vs-code/37155/5 "2022-02-14T14:02:40Z")

</div>

@davidsneighbour I have learned that no setup is perfect and to be a little bit pragmatic …

---

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [February 14, 2022, 2:03pm UTC](https://discourse.gohugo.io/t/setting-up-a-go-template-code-formatter-in-vs-code/37155/6 "2022-02-14T14:03:06Z")

</div>

I have that, but prettier too has no “find the content type of the file” method and is working off regexpes of file names. I think what I need to do is to use the proper filename with the extension in the end of the name being the proper content-type, not \*.html.

It’s a convenience thing. One day I will have learnt it 🙂

---

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [February 14, 2022, 2:04pm UTC](https://discourse.gohugo.io/t/setting-up-a-go-template-code-formatter-in-vs-code/37155/7 "2022-02-14T14:04:06Z")

</div>

Yeah, it would be a boring world if everything would be one size fits it all 🙂 no worries.

---

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [February 14, 2022, 2:05pm UTC](https://discourse.gohugo.io/t/setting-up-a-go-template-code-formatter-in-vs-code/37155/8 "2022-02-14T14:05:30Z")

</div>

By the way, I am not complaining. I just point out the big walls on the side of the race track in case you wish to do cool swerves. VSCode is perfect for working with Hugo and I have yet to meet another program that is of better use.
