{"id":50,"date":"2024-08-30T21:49:50","date_gmt":"2024-08-30T14:49:50","guid":{"rendered":"https:\/\/gotutorial.org\/?page_id=50"},"modified":"2024-08-30T22:06:49","modified_gmt":"2024-08-30T15:06:49","slug":"go-live-reload-nodemon","status":"publish","type":"page","link":"https:\/\/www.gotutorial.org\/go-tutorial\/go-live-reload-nodemon\/","title":{"rendered":"How to Set Up Live Reload for Go Programs with Nodemon"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to set up live reload for Go programs with Nodemon.<\/p>\n\n\n\n<p>When developing Go programs, you often change your code and want those changes to be reflected immediately in the running program.<\/p>\n\n\n\n<p>Instead of manually stopping and restarting your Go program each time you make a code change, you can utilize a tool like <code>nodemon<\/code> to automate this process.<\/p>\n\n\n\n<p>In this tutorial, we&#8217;ll walk you through the steps to set up live reloading for a Go program using <code>nodemon<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1. Install Node.js on your computer<\/h2>\n\n\n\n<p>First, visit the <a href=\"https:\/\/nodejs.org\/\">Node.js official website<\/a>.<\/p>\n\n\n\n<p>Second, download and install the suitable version for your operating system.<\/p>\n\n\n\n<p>Third, open your terminal and verify that Node.js and npm are installed correctly by running the following commands:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">node -v\nnpm -v<\/code><\/span><\/pre>\n\n\n<p>These commands should display the installed versions of Node.js and npm.<\/p>\n\n\n\n<p>Note that npm stands for Node Package Manager, which allows you to install Node.js modules from the NPM repository.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Install Nodemon<\/h2>\n\n\n\n<p>Nodemon is a tool that monitors your GO source files and automatically restarts the GO program when it detects changes.<\/p>\n\n\n\n<p>To install Nodemon, open your terminal and install <code>nodemon<\/code> globally using the following <code>npm install<\/code> command:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">npm install -g nodemon<\/code><\/span><\/pre>\n\n\n<p>The -g flag instructs the command to install nodemon globally on your computer. This means that you can access the nodemon command from any directory.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Creating Up Your Go Program<\/h2>\n\n\n\n<p>Here&#8217;s the <a href=\"https:\/\/gotutorial.org\/go-tutorial\/go-hello-world\/\">Hello, World! program<\/a>. If you haven&#8217;t created it, here&#8217;s the source code.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Go\" data-shcb-language-slug=\"go\"><span><code class=\"hljs language-go\"><span class=\"hljs-keyword\">package<\/span> main\n\n<span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-string\">\"fmt\"<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">func<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span><\/span> {\n    fmt.Println(<span class=\"hljs-string\">\"Hello, World!\"<\/span>)\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Go<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">go<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note that you need to store the code in the main.go file under the hello-world directory.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Test Live Reload<\/h2>\n\n\n\n<p>First, open your terminal and navigate to the hello-world directory:<\/p>\n\n\n\n<p>Second, run the following command to perform live reloads of the main.go program:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">nodemon<\/span> <span class=\"hljs-selector-tag\">--exec<\/span> <span class=\"hljs-selector-tag\">go<\/span> <span class=\"hljs-selector-tag\">run<\/span> <span class=\"hljs-selector-tag\">main<\/span><span class=\"hljs-selector-class\">.go<\/span> <span class=\"hljs-selector-tag\">--signal<\/span> <span class=\"hljs-selector-tag\">SIGTERM<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this command:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>--exec go run main.go<\/code>: This instructs <code>nodemon<\/code> to execute the command <code>go run main.go<\/code> whenever changes are detected.<\/li>\n\n\n\n<li><code>--signal SIGTERM<\/code>: This option instructs <code>nodemon<\/code> to send a <code>SIGTERM<\/code> signal to the Go process when it needs to restart. It allows the Go program to handle a graceful shutdown if necessary.<\/li>\n<\/ul>\n\n\n\n<p>With <code>nodemon<\/code> running, you can make code changes to your <code>main.go<\/code> file and watch the output message. For example, you can change the message to something like <code>Hello, Go!<\/code><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">fmt.Println(<span class=\"hljs-string\">\"Hello, Go!\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Save the file, and you&#8217;ll see that <code>nodemon<\/code> automatically detects the change, stops the running Go process, and starts it again with the updated code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 5: Create a Nodemon Configuration File (Optional)<\/h2>\n\n\n\n<p>If you want to customize <code>nodemon<\/code>, you can create a <code>nodemon.json<\/code> configuration file in the root of your project with the following code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JSON \/ JSON with Comments\" data-shcb-language-slug=\"json\"><span><code class=\"hljs language-json\">{\n  <span class=\"hljs-attr\">\"watch\"<\/span>: &#91;<span class=\"hljs-string\">\"*.go\"<\/span>],\n  <span class=\"hljs-attr\">\"exec\"<\/span>: <span class=\"hljs-string\">\"go run main.go\"<\/span>,\n  <span class=\"hljs-attr\">\"signal\"<\/span>: <span class=\"hljs-string\">\"SIGTERM\"<\/span>,\n  <span class=\"hljs-attr\">\"ext\"<\/span>: <span class=\"hljs-string\">\"go\"<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JSON \/ JSON with Comments<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">json<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this file:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>\"watch\": [\"*.go\"]<\/code>: watch all <code>.go<\/code> files in the current directory.<\/li>\n\n\n\n<li><code>\"exec\": \"go run main.go\"<\/code>: execute the <code>go run main.go<\/code> command when nodemon detects the code changes in any <code>.go<\/code> files.<\/li>\n\n\n\n<li><code>\"signal\": \"SIGTERM\"<\/code>: The signal to send to the process when restarting.<\/li>\n\n\n\n<li><code>\"ext\": \"go\"<\/code>: File extensions for the nodemon to monitor.<\/li>\n<\/ul>\n\n\n\n<p>After having this file, you can simply run the <code>nodemon<\/code> without any arguments because it&#8217;ll use the settings from nodemon.json:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">nodemon<\/code><\/span><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use <code>nodemon<\/code> to auto-reload your Go program whenever you make changes.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<div class=\"wth-question\">Was this tutorial helpful?<\/div>\n\t<div class=\"wth-thumbs\">\n\t\t<button\n\t\t\tdata-post=\"50\"\n\t\t\tdata-post-url=\"https:\/\/www.gotutorial.org\/go-tutorial\/go-live-reload-nodemon\/\"\n\t\t\tdata-post-title=\"How to Set Up Live Reload for Go Programs with Nodemon\"\n\t\t\tdata-response=\"1\"\n\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t>\n\t\t\t<svg\n\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\tfill=\"none\"\n\t\t\t\tstroke=\"currentColor\"\n\t\t\t\tstroke-width=\"2\"\n\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t>\n\t\t\t\t<path\n\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t><\/path>\n\t\t\t<\/svg>\n\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t<\/button>\n\n\t\t<button\n\t\t\tdata-response=\"0\"\n\t\t\tdata-post=\"50\"\n\t\t\tdata-post-url=\"https:\/\/www.gotutorial.org\/go-tutorial\/go-live-reload-nodemon\/\"\n\t\t\tdata-post-title=\"How to Set Up Live Reload for Go Programs with Nodemon\"\n\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t>\n\t\t\t<svg\n\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\tfill=\"none\"\n\t\t\t\tstroke=\"currentColor\"\n\t\t\t\tstroke-width=\"2\"\n\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t>\n\t\t\t\t<path\n\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t><\/path>\n\t\t\t<\/svg>\n\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t<\/button>\n\t<\/div>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you will learn how to set up live reload for Go programs with Nodemon. When developing Go programs, you often change your code and want those changes to be reflected immediately in the running program. Instead of manually stopping and restarting your Go program each time you make a code change, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":10,"menu_order":34,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-50","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/pages\/50","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/comments?post=50"}],"version-history":[{"count":8,"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/pages\/50\/revisions"}],"predecessor-version":[{"id":61,"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/pages\/50\/revisions\/61"}],"up":[{"embeddable":true,"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/pages\/10"}],"wp:attachment":[{"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/media?parent=50"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}