{"id":348,"date":"2024-09-21T09:34:55","date_gmt":"2024-09-21T02:34:55","guid":{"rendered":"https:\/\/gotutorial.org\/?page_id=348"},"modified":"2024-09-21T09:37:35","modified_gmt":"2024-09-21T02:37:35","slug":"go-live-reload-air","status":"publish","type":"page","link":"https:\/\/www.gotutorial.org\/go-tutorial\/go-live-reload-air\/","title":{"rendered":"How to Set up Live Reload for Go Programs using Air"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: this tutorial will teach you how to use the air utility to set up a live reload for Go programs to boost your productivity and streamline your development workflow<\/p>\n\n\n\n<p><strong>Step 1<\/strong>. Open your terminal and create a new project directory:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">mkdir airdemo<\/code><\/span><\/pre>\n\n\n<p><strong>Step 2<\/strong>. Navigate to the project directory:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">cd airdemo<\/code><\/span><\/pre>\n\n\n<p><strong>Step 3<\/strong>. Create main.go file with the following code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">package main\n\n<span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-string\">\"fmt\"<\/span>\n\nfunc main() {\n\tfmt.Println(<span class=\"hljs-string\">\"Hello, World!\"<\/span>)\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><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><strong>Step 4<\/strong>. Run the <code>go init<\/code> command:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">go mod init airdemo<\/code><\/span><\/pre>\n\n\n<p>This will create a <code>go.mod<\/code> file that stores the Go versions and the project&#8217;s dependencies.<\/p>\n\n\n\n<p><strong>Step 5<\/strong>. Install the air utility by running this command from the terminal:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">go install github.com\/air-verse\/air@latest<\/code><\/span><\/pre>\n\n\n<p><strong>Step 6<\/strong>. Create the air configuration file:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">air init<\/code><\/span><\/pre>\n\n\n<p>This command will create the <code>.air.toml<\/code> file that stores the air configuration parameters. <\/p>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>A sample .air.toml file on Windows<\/summary><pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">root = <span class=\"hljs-string\">\".\"<\/span>\ntestdata_dir = <span class=\"hljs-string\">\"testdata\"<\/span>\ntmp_dir = <span class=\"hljs-string\">\"tmp\"<\/span>\n\n&#91;build]\n  args_bin = &#91;]\n  bin = <span class=\"hljs-string\">\"tmp\\\\main.exe\"<\/span>\n  cmd = <span class=\"hljs-string\">\"go build -o .\/tmp\/main.exe .\"<\/span>\n  delay = <span class=\"hljs-number\">1000<\/span>\n  exclude_dir = &#91;<span class=\"hljs-string\">\"assets\"<\/span>, <span class=\"hljs-string\">\"tmp\"<\/span>, <span class=\"hljs-string\">\"vendor\"<\/span>, <span class=\"hljs-string\">\"testdata\"<\/span>]\n  exclude_file = &#91;]\n  exclude_regex = &#91;<span class=\"hljs-string\">\"_test.go\"<\/span>]\n  exclude_unchanged = <span class=\"hljs-literal\">false<\/span>\n  follow_symlink = <span class=\"hljs-literal\">false<\/span>\n  full_bin = <span class=\"hljs-string\">\"\"<\/span>\n  include_dir = &#91;]\n  include_ext = &#91;<span class=\"hljs-string\">\"go\"<\/span>, <span class=\"hljs-string\">\"tpl\"<\/span>, <span class=\"hljs-string\">\"tmpl\"<\/span>, <span class=\"hljs-string\">\"html\"<\/span>]\n  include_file = &#91;]\n  kill_delay = <span class=\"hljs-string\">\"0s\"<\/span>\n  log = <span class=\"hljs-string\">\"build-errors.log\"<\/span>\n  poll = <span class=\"hljs-literal\">false<\/span>\n  poll_interval = <span class=\"hljs-number\">0<\/span>\n  post_cmd = &#91;]\n  pre_cmd = &#91;]\n  rerun = <span class=\"hljs-literal\">false<\/span>\n  rerun_delay = <span class=\"hljs-number\">500<\/span>\n  send_interrupt = <span class=\"hljs-literal\">false<\/span>\n  stop_on_error = <span class=\"hljs-literal\">false<\/span>\n\n&#91;color]\n  app = <span class=\"hljs-string\">\"\"<\/span>\n  build = <span class=\"hljs-string\">\"yellow\"<\/span>\n  main = <span class=\"hljs-string\">\"magenta\"<\/span>\n  runner = <span class=\"hljs-string\">\"green\"<\/span>\n  watcher = <span class=\"hljs-string\">\"cyan\"<\/span>\n\n&#91;log]\n  main_only = <span class=\"hljs-literal\">false<\/span>\n  time = <span class=\"hljs-literal\">false<\/span>\n\n&#91;misc]\n  clean_on_exit = <span class=\"hljs-literal\">false<\/span>\n\n&#91;proxy]\n  app_port = <span class=\"hljs-number\">0<\/span>\n  enabled = <span class=\"hljs-literal\">false<\/span>\n  proxy_port = <span class=\"hljs-number\">0<\/span>\n\n&#91;screen]\n  clear_on_rebuild = <span class=\"hljs-literal\">false<\/span>\n  keep_scroll = <span class=\"hljs-literal\">true<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><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><\/details>\n\n\n\n<p>Step 7. Run the air command on your terminal:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">air<\/code><\/span><\/pre>\n\n\n<p>The output will look like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">  __    _   ___  \n \/ \/\\  | | | |_) \n\/_\/--\\ |_| |_| \\_ v1<span class=\"hljs-number\">.52<\/span><span class=\"hljs-number\">.3<\/span>, built with Go go1<span class=\"hljs-number\">.23<\/span><span class=\"hljs-number\">.0<\/span>\n\nwatching .\n!exclude tmp\nbuilding...\nrunning...\nHello, World!\nProcess <span class=\"hljs-keyword\">Exit<\/span> with Code <span class=\"hljs-number\">0<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Step 8. Change the code in the <code>main.go<\/code>, the Air will automatically detect the change and rebuild the program:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">package main\n\n<span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-string\">\"fmt\"<\/span>\n\nfunc main() {\n\tfmt.Println(<span class=\"hljs-string\">\"Hi there!\"<\/span>)\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><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>Air will produce the following output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">main.go has changed\nbuilding...\nrunning...\nHi there!\nProcess Exit <span class=\"hljs-keyword\">with<\/span> Code <span class=\"hljs-number\">0<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><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><\/p>\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=\"348\"\n\t\t\tdata-post-url=\"https:\/\/www.gotutorial.org\/go-tutorial\/go-live-reload-air\/\"\n\t\t\tdata-post-title=\"How to Set up Live Reload for Go Programs using Air\"\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=\"348\"\n\t\t\tdata-post-url=\"https:\/\/www.gotutorial.org\/go-tutorial\/go-live-reload-air\/\"\n\t\t\tdata-post-title=\"How to Set up Live Reload for Go Programs using Air\"\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: this tutorial will teach you how to use the air utility to set up a live reload for Go programs to boost your productivity and streamline your development workflow Step 1. Open your terminal and create a new project directory: Step 2. Navigate to the project directory: Step 3. Create main.go file with the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":10,"menu_order":33,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-348","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/pages\/348","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=348"}],"version-history":[{"count":7,"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/pages\/348\/revisions"}],"predecessor-version":[{"id":356,"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/pages\/348\/revisions\/356"}],"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=348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}