Set up Go Project
Initialize Go Module and the main.go file
# Create the project folder and navigate into it
md projectNorthWind; cd projectNorthWind
# Create a Go module
go mod init projectNorthWind
# Add the main.go file
new-item main.go
# Let's add some boilder plate code to main.go
package main
import (
)
# Empty {} func main is fine. However, air won't complete the build without some code in the main func.
func main () {
log.Println(">>> STARTING API SERVER")
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal(err)
}
}
Run the Go project to create the go.sum file
go run .
✔️ Outputs: go.mod file, main.go file
go.mod file containing the module (project name) and go version.
❓ Stopping Point
- What does "mod" stand for? What about "sum"?
mod: module (project name) sum: summary of go dependencies - Purpose of the mod file and sum file? mod: go version info and module name sum: dependencies are listed, close to package.json in node projects.
Optional (Thank me later) - Install air for hot reloading during development
It's really annoying to run go run . every time there is a change. For that, use Air that automatically reloads the application whenever it detects a change.
Install Air globally using the following command:
go install github.com/cosmtrek/air@latest
Initiate Air in the project directory.
air init
Output: This will create a .air.toml file in the project directory
To use, simply run `air` in the terminal. It will automatically detect changes and reload the application.
```bash
air
Output: This will create a tmp folder and main.exe file
## Let's Review the Folder Structure
```bash
project-northwind/
│──go.mod (created by `go mod init`)
├──main.go (manually created)
├──README.md (manually created)
├──cmd/(created by `air`)
│ └──tmp/ (created by `air`)
│ └──main.exe (created by `air`)
│ └──build-errors.log (created by successful end of `air` build)
└── .air.toml (created by `air init`)