Beginner question: can I use golang packages within my Hugo project?

Hello! I’m before used Next.js for my personal websites. And it’s amazing because within my Next.js project I can install and use any React.js libraries. That means, I’m able to use the whole React.js ecosystem.

Please, can someone tell me. Can I import and use Golang packages within Hugo project? Also, can I create and write custom golang scripts within Hugo, or import them within markdown files?

No.

No.

Use Hugo’s functions and methods within templates and partials. If you need to insert logic into your markdown, use shortcodes.

Thank you very much for the answer! Please, let me ask one more question.

I’m still able to create a Golang file within my Hugo folder? Something like script.go, and write custom script what I need, and export the result as JSON for example. And then just work with JSON, as data files with Hugo features?

Sure. You can put anything (assuming no conflicts) in the root of your project folder.

You can pull data into your site via templates, partials, or shortcodes. But you cannot create pages from data.

Hi @zhandosweb

I am having similar questions being a nextjs user before Hugo. I was using the example contact form to replace the current php form and I place the form script in static folder of Hugo.

I know something we struggling to understand is since Hugo is built with golang why cant we just add a golang script in Hugo similar to nextjs can do with js :stuck_out_tongue:

if I want to add a script to do authentication or add a contact form script does this mean I have to abandon Hugo or there is a way in which backend golang developer can make use of pages generated by Hugo?

have you worked out anything since you seems to be much older in here thanks

FYI Golang is not a scripting language like JavaScript or Python or Ruby, etc. It is a compiled language like C or Rust. That means to add Go code to Hugo one has to compile it into Hugo, which is quite a different beast than adding a script to a scripting language. It’s not something that can be done ‘on-the-fly’ for example.

In addition, Go does not support dynamic libraries; everything that the binary will execute is compiled at build time. For things liked embedded Dart Sass, one actually execute both the Hugo binary and (via Hugo spawning a new process) the Embedded Dart Sass binary, which communicate with either other using a protocol over (IIRC) pipes.

For contact forms and other non-static behavior one needs likes like Netlify Functions or AWS Lambda functions, or webhooks, or a third party service, or a microservice running on a VPS that handles the dynamic part, or some other non-Hugo solution.

Hugo is a static site generator. That means it generates unchanging pages. They may have JavaScript, but JS doesn’t touch what is on the server. If you use AJAX to a dynamic service, you can have actions that affect a server, but that is not part of Hugo, that is an additional service (of which I gave a few examples, above).

Hope that clarifies.

1 Like

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