Skip to main content
  1. Posts/

Go Tutorials Part 1: How I setup my Go Apps

·388 words·2 mins·
Technology Golang
Joshua Blais
Author
Joshua Blais

I have been using go lang and the Echo framework for all of my app builds over the past six months and can’t see myself ever returning to a javascript or python backend.

Here is how I set up a very basic backend in 4 simple steps:

Initial Setup
#

Create a Directory for the project to live, create main.go & .env file and go mod init:

mkdir projectname
cd projectname

touch main.go
touch .env
touch .gitignore
go mod init github.com/username/projectname

Within my .gitignore I will include my .env file, any tmp files, and the _templ.go files that are generated so as to not create too much of a mess in my version history.

Install Dependencies
#

go get ........

// Some packages I use:
https://github.com/joho/godotenv
https://github.com/labstack/echo
https://github.com/a-h/templ

Main.go file initial
#

package main

import (

)

func main() {

}

Setup Makefile/Taskfile with reqs to run app, etc. as well as my .air.toml file for hot reloading
#

.air.toml:

root = "."
tmp_dir = "tmp"

[build]
  bin = "./tmp/main"
  cmd = "templ generate && npx tailwindcss -i view/css/app.css -o public/styles.css && go build -o ./tmp/main ."

  delay = 1000
  exclude_dir = ["assets", "tmp", "vendor"]
  exclude_file = []
  exclude_regex = [".*_templ.go"]
  exclude_unchanged = false
  follow_symlink = false
  full_bin = ""
  include_dir = []
  include_ext = ["go", "tpl", "tmpl", "templ", "html"]
  kill_delay = "0s"
  log = "build-errors.log"
  send_interrupt = false
  stop_on_error = true

[color]
  app = ""
  build = "yellow"
  main = "magenta"
  runner = "green"
  watcher = "cyan"

[log]
  time = false

[misc]
  clean_on_exit = false

Makefile

run: build
	@./bin/appname

install:
	@go install github.com/a-h/templ/cmd/templ@latest
	@go get ./...
	@go mod vendor
	@go mod tidy
	@go mod download
	@npm install -D tailwindcss
	@npm install -D daisyui@latest

build:
	@npx tailwindcss -i view/css/app.css -o public/styles.css
	@templ generate view
	@go build -o bin/appname main.go

up: ## Database migration up
	@go run cmd/migrate/main.go up

reset:
	@go run cmd/reset/main.go up

down: ## Database migration down
	@go run cmd/migrate/main.go down

migration: ## Migrations against the database
	@migrate create -ext sql -dir cmd/migrate/migrations $(filter-out $@,$(MAKECMDGOALS))

seed:
	@go run cmd/seed/main.go

You now have a skeleton of a go app that you can use to create whatever it is that you want.

In the next installment of this guide, I will show you how I use Templ and tie in Tailwindcss/DaisyUI and other various services to build my apps.